The assailant, armed with a knife and an ax, was shot dead by police after a chase and confrontation, Bavarian Interior Minister Joachim Herrmann said at a news conference early Tuesday.
He identified the attacker as a 17-year-old Afghan man living in Ochsenfurt, Bavaria. The teen came to Germany as an unaccompanied minor a year ago, Herrmann said. He was taken in by a foster family two weeks ago, he said.
A hand-drawn flag resembling the one used by ISIS was found in the room of the Afghan man, Herrmann said.
"The first emergency call that the police headquarters received was about a witness who said that the attacker carried out attacks causing serious injuries and he had called out Allahu Akbar," Herrmann said.
Amaq, the ISIS-linked news agency, released a video Tuesday purporting to show the attacker.
In the 2 minute, 20 second clip, the young man, speaking in Pashto, waves a knife at the camera and says: "I am a soldier of the caliphate".
German prosecutors said they are working to confirm the identity of the youth featured in the video.
"We have no indications that he was already radicalized before he came to Germany," Chief Prosecutor Erik Ohlenschlager said at a news conference Tuesday.
ISIS didn't really have a presence in Afghanistan until it first emerged last year
in the country's east, gaining ground often among disaffected Taliban and Afghan youth.
Police are trying to investigate the background of the parents of the attacker who are still in Afghanistan.
"This is a big mosaic puzzle right now and we will do everything to pull information together in order to assess his motivation," Herrmann said.
Besides the hand-drawn or painted flag, police found notes in Pashto written in Arabic and Latin characters in the assailants' room, Herrmann said.
Prosecutor Lothar Kohler read out a phrase from that note, which could be construed as a farewell letter to his father. It said, in part: "And now pray for me that I can avenge myself on the unbelievers and pray for me that I will go to heaven."
Kohler said it was clear that the attack was "politically motivated". He said the assailant posted cryptic messages on the Internet a few hours before the attack, talking about enemies of Islam.
Investigators are examining the assailant's mobile phone. Prosecutors said that he had learned that a friend had been killed in Afghanistan over the weekend.
The attack took place as the train was traveling between Treuchtlingen and Wurzburg.
"He went to the toilet and then (came out) wielding the ax and the knife," Herrmann said.
Video from inside the train shows extensive blood on the floor.
The train made an emergency stop short of the station at Wurzburg-Heidingsfeld and the assailant jumped out of the train, regional police spokesman Lt. Fabian Hench said.
"They (police) chased him up, confronted him, the attacker then was very aggressive and attacked the police officers with his ax," Herrmann said. "And the police opened fire. The attacker then was shot dead."
Germany absorbed more than one million refugees last year. Some have been concerned over the presence of terror groups in the country -- both the potential for attackers to slip in with migrants and the concern that they may be able to radicalize disaffected youths.
Three Syrian men were arrested last month
on suspicions that they were planning to carry out a mass casualty attack in Dusseldorf.
Many 'in shock' after incident
Some 14 to 25 people were classified as "in shock" and treated at the scene after the attack, according to Alexander Gross, a police officer in Bavaria.
Four passengers on the train -- members of the same family and tourists from Hong Kong -- were injured, and one person is in critical condition, Herrmann said.
Another woman was wounded after the assailant jumped from the train and fled, prosecutors said. She is also in serious condition.
There were about 20 to 30 people on the train, according to Gross.
A Deutsche Bahn spokesperson confirmed that train service between the two stations has resumed.
German Interior Minister Thomas de Maiziere released a statement expressing shock at the "senseless act of indiscriminate violence."
"Security officials in federal and state governments are very vigilant, and we will do everything in our power to prevent such acts," the statement said.