A 17-year-old Afghan asylum seeker shouted "Allahu akbar" ("God is great") during an axe and knife attack on a train in southern Germany in which at least five people were injured, a top security official has said.

Bavaria's interior minister Joachim Herrmann said on public television that investigators raiding the teenager's room also found a hand-painted flag of the Islamic State group.

The attacker was shot and killed by a special police unit which coincidentally happened to be nearby.

"Even during the first emergency call, a witness said that the attacker was shouting 'Allahu akbar' on the train," Mr Herrmann told ZDF Television. "Also, during the search of his room, a hand-painted IS flag was found."

Mr Herrmann said it was still too early to draw conclusions about the attacker's motive.

The attacker, who was not identified by the authorities, came to Germany two years ago as an unaccompanied minor, and applied for asylum in March of last year. He lived in a home for young refugees until two weeks ago when he was placed with a foster family in the Wuerzburg area. Investigators were talking to the foster family, witnesses and the attacker's friends.

The attacker injured at least four people on the train near Wuerzburg-Heidingsfeld on Monday night, and also a woman outside the train as he fled.

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, and more than a dozen were treated for shock.

On Tuesday morning, officers could be seen removing the attacker's body from the scene.

Mr Herrmann said at least two victims - members of a Chinese tourist family - are still in a critical condition.

German officials have not yet identified the victims, but Hong Kong's immigration department said on Tuesday that among those injured in the attack were four members of a family of five from the Chinese territory. The department said it is working to provide assistance to the family but did give not give details of their injuries.

The South China Morning Post newspaper reported that the family members hurt included the 62-year-old father, 58-year-old mother, 27-year-old daughter and her 31-year-old boyfriend. A 17-year-old son was not hurt.

Hong Kong's most senior official, chief executive Leung Chun-Ying, condemned the attack and extended his sympathies to the victims and their families.

Germany last year registered more than one million asylum seekers entering the country, including more than 150,000 Afghans.

In May, a man stabbed four people at a German railway station in a random early-morning attack in Grafing near Munich. One man later died.

The attacker, a German citizen, also shouted "Allahu akbar" during the attack, but authorities found no evidence of links to Islamic extremists. The attacker was later sent to a psychiatric hospital.