An Afghan refugee yelling “Allahu akbar” went on a bloody ax rampage aboard a German commuter train Monday, injuring at least four people before being fatally shot by cops as he attempted to flee.

The attacker, identified as a 17-year-old Afghan male, left three people seriously injured after he lunged at them with the ax and a knife, witnesses said.

One witness told the DPA news agency that the railway station in Wurzburg near Munich looked “like a slaughterhouse” after the attack, with blood smeared across the floor.

Bavarian Interior Minister Joachim Herrmann said authorities were still investigating a motive and looking to verify witness accounts that the suspect had yelled out “an exclamation” during the rampage.

Germany last year registered more than 1 million migrants entering the country, including more than 150,000 Afghans, but it was not immediately clear whether the suspect was among them or had been in the country for a longer time.

Herrmann said initial information was that the suspect came to Germany as an unaccompanied minor and had lived in the area for some time, initially at a refugee facility.

With Post wire services