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.
Bavarian regional prosecutor Erik Ohlenschlager said the boy was a devout Muslim and wanted to get revenge on "infidels" who had harmed his Muslim friends. He accepted that his own death was a possibility.
Mr Ohlenschlager said the attack was "definitely politically motivated".
But Bavaria's Interior Minister Joachim Herrmann said there was no indication the teenager had direct contact with IS.
The teenager reportedly shouted the Islamic phrase "Allahu akbar" ("God is great") during the attack.
A police official said on Tuesday that two of the five people injured were in a "life-threatening" condition.
Inside the carriage, a 62-year-old man, his 58-year-old wife, their daughter, 27, and her boyfriend, 27, were attacked, the South China Morning Post reports. They were from Hong Kong. The 17-year-old son travelling with them was not hurt, it said.
A source told the paper the father and boyfriend had tried to protect the other members of the group.
Another woman was injured outside the train as the man fled. Fourteen people were treated for shock.
A news agency with links to IS said the boy had launched the attack "in answer to the calls to target the countries of the coalition fighting Islamic State".
The axe attack comes days after a lorry ploughed into a crowd in Nice in France, killing 84 people. IS said one of its followers had carried out that attack.
Mr Herrmann said those who had interacted with the young man in recent months described him as calm and quiet and they could not understand his actions.
The teenager had gone to the mosque "on special occasions", he said, but no-one had noticed any radical behaviour and there were no signs yet of a direct link to jihadist networks.
He had a placement in a bakery and was likely to secure paid employment soon.
Mr Herrmann said there was no indication Chinese citizens had been specifically targeted.
He also defended the police who shot the attacker, saying the teenager had run at officers brandishing the axe.
The Afghan teenager had been living with a foster family since moving from a refugee centre in the town two weeks ago.
Last year Germany registered more than one million migrants, including more than 150,000 Afghans, although the number has slowed dramatically this year since new EU measures were taken to stop the flow.
An ever-present division - Damien McGuinness, BBC News, Berlin
To get a feeling for the wider ramifications of this attack in Germany, you just need to take a quick look at Twitter in German. Racist and xenophobic comments against asylum seekers compete with equally impassioned arguments in support of refugees - including a tweet by Green MP Renate Kuenast asking why police had killed the attacker rather then injuring him.
This has provoked in turn another storm online, saying the perpetrator is getting more sympathy than the victims.
Over the past few months, since the EU agreed a deal with Turkey, numbers of migrants coming to Germany have dropped dramatically. And the issue has vanished from the front pages. The debate had shifted to a more nuanced one about how best to integrate the new arrivals. And the ferocity, fear and sometimes hate appeared to have dissipated. But the news that the attacker was an unaccompanied underage refugee shows that the division running through Germany about Angela Merkel's stance on refugees is still very much there.
Were you on the train? Have you been affected by the incident? Tell us your experiences by emailing email@example.com.
You can also contact us in the following ways: