An Islamic State flag has been found in the room of an Afghan teenager accused of carrying out an axe attack on a train in northern Bavaria, according to German officials.

Five people were injured in the attack on Monday evening before the attacker was shot dead by police. Isis claimed responsibility in a statement on its Amaq news agency on Tuesday morning – its first such claim for an attack in Germany.

Bavaria’s interior minister, Joachim Herrmann, said the suspect was a 17-year-old Afghan asylum seeker, and that he shouted “Allahu Akbar” during the attack.

Herrmann said on Tuesday that investigators raiding his room also found a handpainted Isis flag.

He told ZDF Television the alleged attacker came to Germany two years ago as an unaccompanied minor, and applied for asylum in March. He lived in a home for teenage refugees until two weeks ago when he was placed with a foster family.

Two of the victims of the attack are in a critical condition, Hermann added. According to the South China Morning Post, the four victims of the attack are all members of the same family from Hong Kong, namely the father, mother, daughter and her boyfriend. According to the newspaper, a fifth member of the group, the 17-year-old son, managed to escape unharmed.

After passengers managed to alert the driver, the train was stopped in the Heidingsfeld district of Würzburg and the attacker initially managed to flee from the carriage on foot, Herrmann said.

A police taskforce that happened to be in the vicinity then pursued the attacker, shooting dead the teenager, who was carrying an axe and a knife when he had attacked members of the unit.

The man reportedly attacked passengers on the regional train travelling between the town of Treuchtlingen and Würzburg. Fourteen other passengers were reportedly in a state of shock and receiving treatment by specialists.

The interior ministry could not confirm whether any of the victims were in a life-threatening condition.

The train line between Ochsenfurt and Würzburg remained closed while police investigated. Police initially said there was no indication of a motive, and they were treating the attacker as a lone individual, citing witness reports.

There have not been any attacks with an explicit terrorist motive in Germany, which has been at the heart of the refugee crisis over the past year. In November, a football friendly between Germany and Holland in Hanover was cancelled after a terrorist attack tip-off, with the interior minister, Thomas de Maiziére, saying there had been a “concrete threat” of an attack.