"It appears that there is a radical Islamist background to the act," they said in a statement with reference to the attack on Friday.
The man, named as Ahmad A., had "self- radicalised", after having toyed with the extremist ideology for some time, said prosecutors.
Two days before he launched the attack, the suspect "finally decided to adopt a corresponding lifestyle," they said.
"On the day of the act, he resolved to commit an attack with the hope that he would die as a martyr," added prosecutors.
Investigators have however not found any indications suggesting that he was a member of jihadist groups like the Islamic State organisation.
On Friday afternoon, the suspect entered a supermarket on a lively street in Hamburg and took a kitchen knife measuring around 20 cm (eight inches) from the shelves.
"He ripped off the packaging and then suddenly brutally attacked a 50-year-old man who later died," said deputy police chief Kathrin Hennings.
He later wounded two more men in the supermarket before fleeing, hurting four other people along the way, before he was overpowered by passers-by.
Witnesses told AFP the man brandished the bloodied knife, shouting "Allahu Akbar" ("God is Greatest") as he fled the scene, but that bystanders gave chase and flung chairs to stop him.