German police shot dead a teenager on Monday night after he attacked passengers on a train with an axe and a knife, seriously wounding three people, officials said.

Several other people were also injured in the attack on a regional train near the southern city of Wuerzburg, police said, adding the teenager was killed as he tried to flee.

Joachim Herrmann, the Interior Minister of Bavaria state, said the attacker was a 17-year-old Afghan who had lived in nearby Ochsenfurt.

Mr Herrmann said the assailant had arrived as an unaccompanied minor in Germany and had lived at first in a shelter and then more recently with a foster family.

"It is quite probable that this was an Islamist attack," he said, adding the assailant had shouted "Allahu akbar", which is Arabic for "God is greatest".

However he stressed the investigation was ongoing and the teenager appeared to have acted alone.

The attack happened about 9:15pm (local time) on the train, which runs between Treuchlingen and Wuerzburg in Bavaria.

"Shortly after arriving at Wuerzburg, a man attacked passengers with an axe and a knife," a police spokesman said.

"Three people have been seriously injured and several others lightly injured.

"The perpetrator was able to leave the train, police left in pursuit, and as part of this pursuit, they shot the attacker and killed him."

An eyewitness who lives next to the railway station told DPA news agency the train, which had been carrying about 25 people, looked "like a slaughterhouse" after the attack, with blood covering the floor.

The man, who declined to give his name, said he saw people crawl from the carriage and ask for a first-aid kit as other victims lay on the floor inside.

In May, a mentally unstable 27-year-old man carried out a similar knife attack on a regional train in the south, killing one person and injuring three others.

Early reports suggested he had yelled "Allahu Akbar" but police later said there was no evidence pointing to a religious motive. He is being held in a psychiatric hospital.

AFP