A 17-year-old Afghan asylum seeker received word that a friend had died in his home country days before he went on a rampage with an axe and a knife on a German train, wounding five, investigators said.
He vowed in a note that he would "take revenge on these infidels".
The Islamic State (IS) group claimed responsibility for the Monday night attack, but authorities say so far they have found no direct links to the group and believe the suspect, whose name was not released, was self-radicalised.
German officials did not identify the victims, but Hong Kong's immigration department said that among those injured in the attack were four members of a family of five from the southern Chinese city. At least two of the five wounded were in a life-threatening condition.
Investigator Lothar Koehler said the teenager's motivation appeared to be Islamic extremism based upon a passage, found among various notes in his apartment, which read: "Pray for me that I can take revenge on these infidels and pray for me that I will go to heaven."
In the premeditated attack, the suspect boarded the regional train after 9pm near the Bavarian city of Wuerzburg with an axe and a knife concealed in a bag, according to Bamberg prosecutor Erik Ohlenschlager. He said that the suspect had learned on Saturday that a friend had died in Afghanistan, but did not immediately give more details about the possible link.
On the train, the suspect initially encountered an employee from the asylum shelter where he had lived until two weeks ago when he moved in with a foster family, and when she said something to him he did not respond but left for another train car, Mr Ohlenschlager said.
He then concealed himself in a bathroom, and armed himself with his weapons.
"Then, without warning, he attacked the passengers using great force on their bodies and their heads," Mr Ohlenschlager said.
In an emergency call from the train, the suspect could be heard in the background shouting "Allahu akbar," Arabic for "God is great," as he hacked and slashed.
Witnesses said the interior of the train was covered with blood and looked "like a slaughterhouse," the German news agency DPA reported. About 30 passengers were on the train at the time. More than a dozen were treated for shock.
After the train made an emergency stop, the suspect fled and a few hundred metres away encountered two women walking a dog. He attacked one from behind, saying "I'll finish you" as he hit her at least twice in the face with the axe.
Shortly after, he encountered a police SWAT team that had been in the area on another mission but had been redirected, jumping out at them from bushes, brandishing his axe. He was shot and killed.
Mr Ohlenschlager said that at least two victims were suffering from "acute life-threatening" wounds, including the woman attacked outside the train.
Though the IS group claimed responsibility through its Aamaq news agency and a hand-painted IS flag was found in the suspect's apartment, Bavarian Interior Minister Joachim Herrmann said the suspect's handwritten notes indicated he may have been self-radicalised and there was "no indication" he was directly connected to the group.
Aamaq released a video that purported to show the attacker, in which a young man waves a knife towards the camera and says: "I will behead you with these knives and break your necks with these axes."
In the video, the man also says in Pashto he is a soldier "of the Islamic State and will be carrying out an attack in Germany today".
Mr Ohlenschlager said investigators were aware of a video and are currently evaluating it to determine whether it was made by the suspect.
Mr Herrmann said people close to the attacker told investigators he had seemed like a calm person, not overtly religious or an extremist.
He said investigators were still looking into the evidence found in the teenager's room, saying it could be possible that the notes included a farewell letter to his father.
Hong Kong's top official, Chief Executive Leung Chun-Ying, condemned the attack and extended his sympathies to the victims and their families.
Mr Herrmann said it was a tragedy that a "family from Hong Kong comes here as tourists to visit Wuerzburg ... and then becomes victim on a train here in Bavaria in an attack conducted by an offender who came from Afghanistan and who was originally seeking shelter here".
DPA reported that the attacker wounded the 62-year-old father, the 58-year-old mother, their adult daughter and her boyfriend. The teenage son was not hurt. The father and the boyfriend had tried to defend the other family members.
The mayor of Wuerzburg condemned the attack.
"I'm shocked by this horrible act of violence," Christian Schuchardt said, adding that his thoughts were with the victims and other passengers "who have suffered severe injuries on their bodies and souls by this act of craziness".