Prosecutors in Germany say a teenager who attacked train passengers with an axe in Wuerzburg had learnt that a friend had been killed in Afghanistan, and wanted to get revenge.
The 17 year-old, who arrived in Germany a year ago as an unaccompanied refugee, injured four people, two critically, in the attack on Monday evening.
He was shot dead by police as he fled.
The self-styled Islamic State group (IS) has released a video purporting to show him making threats.
In it, a young man brandishing a knife says he is an "IS soldier" preparing for a suicide mission. German officials say they later found a hand-painted IS flag in his room.