The attacker came to Germany as an unaccompanied minor refugee, Bavarian interior minister Joachim Herrmann told state television.
He had been living in Würzburg since March, Die Welt reported, and after staying in refugee accommodation for several months, he had moved to live with a foster family in the nearby town of Ochsenfurt just two weeks ago.
The accommodation was searched on Tuesday, and the foster family questioned. Investigators found a hand-painted Isis flag in his bedroom, and the Isis terror group on Tuesday said that the refugee was one of its "fighters".
"The perpetrator of the stabbing attack in Germany was one of the fighters of the Islamic State," the Isis-linked Amaq news agency said.
The attacker was registered as refugee on 30th June, 2015 in Passau, on the border with Austria, a spokesperson for the Bavarian a spokesperson for Bavarian state police said at a press conference on Tuesday.
At the start of December he made application for asylum, which he received in March this year. He had not been on the radar of the Verfassungsschutz or Bavarian police at all, the spokesperson said.
Friend died in Afghanistan days before the attack
The investigator added that the adolescent had “acted very suspiciously" in recent weeks.
“He was a practising Muslim but he did not go to mosque every day - he followed his religion privately. There are lots of question still to be answered,” he added.
The attacker was active on social networks, but his activity showed no clues that he had Islamist leaning. Twenty-four hours before the attack, however, he made a cryptic posting on social media that made mention of Islam.
There were several texts in his bedroom, one of which investigators believe is a suicide note to his father.
The police investigator then read from the text to prove their suspicion that there was a religious motive for the attack: “Pray for me that I can take vengeance against these unbelievers and pray for me that I go to heaven.”
Investigators have also established that the teenager found out on Friday or Saturday that a friend of his died in Afghanistan, a fact which led to him contacting relations back home throughout the coming days.
At first, police were not able to find his mobile phone but it was finally located between where the train came to a stop and the river bank. Police hope it will give further clues about who the young attacker contacted back in Afghanistan.
“It is difficult to say the significance of this discovery,” the investigator said.
At the same press conference, Würzburg's lead prosecutor Erik Ohlenschlager said that the teen had carried out the attack with a “will to annihilate.”
The prosecutor confirmed earlier reports that the 17-year-old shouted “Allahu Akbar” on several occasions during the attack. This can “clearly be heard” in the background of an emergency call from a person present at the attack.