Police said the suspect was 26 years old and born in the United Arab Emirates. They said he acted alone and he was overpowered by passersby.
Mayor Olaf Scholz said the attack was motivated by "hate" but did not say it was a terror attack.
There are unconfirmed reports that the man shouted "Allahu akhbar", Arabic for "God is great" while attacking people.
Police said they were still seeking to confirm his current nationality. Mr Sholz said he was a failed asylum seeker.
The man attacked customers in the supermarket at random, ran outside and was chased by customers and by people outside the shop. They brought him to the ground before he was detained by plain-clothes police officers.
Mobile phone footage on German media that is purportedly of the attack's aftermath shows several people chasing a man and throwing chairs at him.
A 50-year-old woman and four men aged between 19 and 64 were stabbed, while a 35-year-old man was injured while helping overcome the suspect.
The attack happened in the Barmbek region in the north of the city in a branch of Edeka, Germany's largest supermarket chain.
Police say the man's weapon may have looked like a machete but they are, for now, assuming it was a kitchen knife.
One eyewitness made a gesture about 50cm (20 in) long when asked on German TV how big the knife was.
Asked whether this was a terror attack, a police official told the BBC: "We are at an early stage of the investigation... At the moment we can't rule anything out. We are investigating all avenues."
Speculation in German media said the attack may have been part of a robbery attempt but police said that that motive could not be confirmed.
One eyewitness, who ran from the scene into a nearby shop as the situation unfolded, told German broadcaster NTV that it seemed "hectic and confused, not like a properly planned attack".

Sheffield City Council say the owner of Cake 'R' Us, in Devonshire Street, did not have consent for the work.
It says the shop must be repainted in a colour scheme "more in keeping with its 19th Century characteristics".
Owner David Chan said he had painted the shop in an "act of national pride".
Live updates on this story and others from Sheffield and South Yorkshire
A complaint about the shop, which is in part of the former Wharncliffe Fireclay Works built for John Armitage in 1888, was received in January 2015.
The council wrote to Mr Chan to say the design was "unacceptable" but was told he had "no intention of repainting the shop" and would instead apply for consent. It said though no application has yet been made.
Mr Chan said: "It's the Union Jack. I do not see why it needs to be changed. I support Queen and country."
Asked what he would do if the council took legal action to make him repaint the shop he said: "I will write to the Queen. I do not see anything wrong with it. It's an act of national pride."
A council report said: "The union flag theme that has been applied to the shop front is considered to be visually intrusive and does not respect the original character of the property to which it is attached, due to its contemporary and garish design.
"Whilst the need of the business to advertise its presence and attract custom is recognised, this shop front has a deep fascia and projecting sign for these purposes, and there is no justification for garish and unsympathetic painting of the shop front to serve this purpose."
The report recommends that the council's planning committee, which is due to meet on 26 April, agree to take any "appropriate action including, if necessary, enforcement action and the institution of legal proceedings to secure repainting of the shop front".
A 50-year-old German man has been killed and six people injured in a knife attack in a Hamburg supermarket.
[NEXT_CONCEPT]
A man who painted the front of his Grade II listed shop with a "garish and unsympathetic" union flag is facing possible legal action for breaching planning regulations.