The man, named as Ahmad A, had self-radicalised and had gone on the rampage "with the hope he would die as a martyr," added prosecutors.
The Palestinian suspect had entered a supermarket on a lively Hamburg high street on Friday, snatching from the shelves a 20 cm (eight-inch) knife which he used to slash at people around him. The assault left one dead and six wounded.
It was the first Islamist attack in Germany since Tunisian Anis Amri drove a truck into crowds at a Berlin Christmas market on December 19, killing 12 and injuring 48.
"It appears that there is a radical Islamist background to the act," prosecutors said.
More From This Section
The suspect had toyed with the extremist ideology for some time, and two days before he went on his rampage, he "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.
Germany has been on high alert over the threat of a jihadist assault since Amri's rampage in Berlin, for which the Islamic State group claimed responsibility.
Authorities had said earlier that Ahmad A. Was deemed to be an "Islamist" but not believed to be dangerous enough to warrant being monitored by police.
Hamburg's interior minister Andy Grote had initially said that there could have been an Islamist motive for the attack, the suspect also suffered from "psychological instability".
Like Amri, Ahmad A. Was due to have been deported after his asylum application was rejected by authorities, but the process was held up by a lack of identity documents.
Domestic security expert for Merkel's centre-right alliance, Stephan Mayer, hit out at Hamburg authorities for failing to apply more stringent criteria on rating the danger posed by Islamists.
Marcus Pretzell of the Islamophobic populist party AfD also leapt in, saying that Hamburg happened because of "carelessness on the state's part".
Meanwhile, Germany was celebrating a group of courageous passers-by who helped stop Ahmad A.
As he was fleeing the supermarket, bystanders gave chase, flinging chairs to stop him.