A 17-year-old Afghan refugee wielding an axe and a knife attacked passengers on a train in southern Germany on Monday evening, severely wounding four, before he was shot dead by police, the interior minister for the state of Bavaria said.

ISIS, in a statement issued by its news agency, claimed one of its fighters had perpetrated the attack.

A hand-drawn Islamic State flag was found in the assailant's room, a German television station said, quoting Interior Minister Joachim Herrmann. A German police investigator says that among many notes found in the train attacker's home is one that convinced them that his motivation was Islamic extremism. Lothar Koehler said the passage read "Pray for me that I can take revenge on these infidels and pray for me that I will go to heaven."

German prosecutors say the assailant had learned over the weekend that a friend had been killed in Afghanistan.

Open gallery view Firetrucks and ambulances stand at a road block in Wuerzburg, southern Germany, July 18, 2016. Credit: Karl-Josef Hildenbrand, DPA via AP

German media, citing a spokesman for the Bavarian interior ministry, reported that the perpetrator had shouted "Allahu Akbar" (God is great) during the attack, but neither Herrmann nor Hench said they could confirm that.

"There are witnesses that suggest there may be an Islamic background to this but that is far from clear at this point," Herrmann said.

The attack comes just days after a Tunisian delivery man ploughed a 19-tonne truck into crowds of Bastille Day revelers in the southern French city of Nice, killing 84.

It is likely to deepen worries about so-called "lone wolf" attacks in Europe and could put political pressure on German Chancellor Angela Merkel, who welcomed hundreds of thousands of migrants to Germany over the past year.

"The attacker appears to have been a 17-year-old Afghan who has been living in Ochsenfurt for some time," Herrmann said. "He suddenly attacked passengers with a knife and an axe, critically injuring several. Some of them may now be fighting for their lives."

Police spokesman Fabian Hench said four people had been severely wounded and a fourth slightly injured. Several others were treated for shock.

Wuerzburg police said on its Facebook page that three victims suffered serious injuries, a fourth was slightly injured and 14 more were being treated for shock.

Hong Kong officials said the wounded were four members of a family from the semiautonomous southern Chinese city.

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 fifth family member, a 17-year-old son, was not hurt. The paper did not cite its source.

The attacker fled the train when it halted at a station on the outskirts of Wuerzburg. Herrmann said the man had tried to attack police when he was confronted and had been shot dead.

German media, citing a spokesman for the Bavarian interior ministry, reported that the man had shouted "Allahu Akbar" (God is great) during the attack, but neither Herrmann nor Hench said they could confirm that.

"There are witnesses that suggest there may be an Islamic background to this but that is far from clear at this point," Herrmann said.

Unlike neighbors France and Belgium, Germany has not been the victim of a major attack by Islamic militants in recent years, although security officials say they have thwarted a large number of plots.

Germany welcomed roughly 1 million migrants in 2015, including thousands of unaccompanied minors. Many were fleeing war in countries like Syria, Iraq and Afghanistan.