The teenager reportedly shouted "Allahu Akbar" before injuring four travellers from Hong Kong on Monday evening on a train that runs between Treuchlingen and Wuerzburg. He was shot dead by police as he fled.
An eye witness said the train carriage "looked like a slaughterhouse".
Two of those injured are in critical condition, BBC reported.
The attack comes days after the IS claimed that the truck driver who ploughed through a Bastille Day crowd in Nice, France, killing 84 people and injuring over 200, was a "follower".
Joachim Herrmann, the Interior Minister of the state of Bavaria, said the IS flag was found among the teenager's belongings in his room in Ochsenfurt town.
Herrmann said it was too early to say whether the attacker was a member of the IS group or had become self radicalised.
The teenager, who claimed asylum after travelling to Germany as an unaccompanied minor, was living with the foster family since moving from a refugee centre.
Last year Germany registered more than one million migrants, including more than 150,000 Afghans, although the number has slowed dramatically this year since new EU measures were taken to stop the flow.
Hong Kong Chief Executive Leung Chun-ying condemned the attack and expressed his sympathy to the victims. Immigration officials from the city will accompany family members to Germany.
This website uses cookies.