The announcement, in a bulletin issued in Arabic via its Amaq News Agency, came after the German authorities said that investigators had found a hand-drawn flag of the Islamic State, also known as ISIS or ISIL, in the room of the Afghan teenager.
In its statement, the agency said that the attack had been committed by an “Islamic State fighter,” and said he had acted “in response to the call to target coalition countries” fighting the militant group, although the authorities have not established a firm connection between the teenager and the group.
The Islamic State recently claimed responsibility for the attack in Nice, France, on Friday that left at least 84 people dead, but this time moved much quicker in making its public statement.
The teenager, who arrived from Afghanistan without his parents to apply for asylum last year, carried out the attack alone Monday night, said Joachim Herrmann, interior minister of the southern state of Bavaria, where the attack occurred.
The authorities were trying to determine what motivated the teenager to carry out the attack, which left at least four people wounded, and whether he had direct contact with operatives of the Islamic State or was simply motivated by online propaganda.
“The first emergency call to the police from a witness in the train said that he had shouted ‘Allahu akbar,’ ” Mr. Herrmann told the public broadcaster ZDF. “In searching the room where he last lived, a hand-drawn I.S. flag was found.”
“This must now all be put together in like a big mosaic,” he said, “to figure out what his motivation was and the extent to which he really belonged in an Islamic movement, or whether he became self-radicalized very recently.”
Alexander Gross, superintendent criminal detective of Bavaria’s State Office of Criminal Investigations, said that his office was aware of the Islamic State claim but could in no way comment on its veracity.
“Just because I.S. is claiming this attack does not mean there is anything to it,” Mr. Gross said. “Right now, we have to examine in great detail who he knew and with whom he was in contact in order to create a complete picture” of what motivated him.
After the attack, a passenger pulled the emergency brake on the train, which was traveling to Würzburg from Treuchtlingen, the federal police in Würzburg said.
The teenager then fled the car into a district of Würzburg, where he encountered police officers, including special forces. He lunged at the officers, who responded by opening fire on the young man and killing him, Mr. Herrmann said.
Four of the victims were from the same Hong Kong family, and the territory’s leader, Leung Chun-ying, condemned the attack on Tuesday. Two of the victims are in critical condition, Mr. Herrmann said. Mr. Leung sent representatives from its Berlin trade office and from Hong Kong to Würzburg to follow up and provide assistance.
More than 14,400 unaccompanied minors arrived last year among the more than one million migrants who entered the country. Many live in group homes, but others have been placed in foster families. Mr. Herrmann said the attacker had most recently lived with a family near Würzburg.
Germany has not experienced attacks on the same scale as Belgium or France, but it remains on edge amid threats on social media by Islamist extremists. Several plots have been foiled by the police. In May, a 27-year-old German killed one man and wounded three otherswith a knife while shouting “Allahu akbar” on a commuter train in a suburb of Munich. After questioning him, the authorities said that he had no known links to extremist groups and that they believed he was mentally disturbed.
Fears that terrorists may have entered the country among the hundreds of thousands of migrants have been running high. With the attack coming days after a Tunisian man drove a truck down a packed street on Bastille Day in Nice the attack Monday night could have wider political ramifications.
Germans, who arrived in droves at train stations and bus stops to welcome the migrants as they arrived last year, have grown increasingly fearful as the time has passed. Fears intensified after North African migrants were linked with dozens of sexual assaults on women in Cologne on New Year’s Eve.