KABUL — An Islamic State suicide bomber attacked a crowded mosque during Friday prayers in northern Afghanistan, killing nearly 50 Shiite Muslim worshipers and wounding dozens more, underscoring the growing challenge the extremist group poses to the authority of the Taliban.

It was the latest in a string of attacks on religious facilities and the deadliest since U.S. troops exited the country in August. And it raises questions about the ability of Afghanistan’s new rulers to usher in a stable and secure country after two decades of war.

“I saw more than 40 dead bodies lying around,” said Ghausuddin, a 60-year-old engineer who arrived at the mosque minutes after the blast and spoke on the condition that only his middle name be used, for his safety. “There was blood everywhere. In every family, one or two people were either injured or killed.”

The suicide bomber walked into the male section of the Sayed Abad mosque in Kunduz and detonated his explosives, witnesses said. All of the victims were members of Afghanistan’s Shiite Hazara minority, long persecuted by Sunni Muslim extremist groups, including the Taliban. Bilal Karimi, the deputy Taliban spokesman, said Friday night that the blast had killed 46 worshipers and injured another 143.

The Islamic State-Khorasan, the Afghanistan branch of the Syria- and Iraq-based network, claimed responsibility for the attack late Friday in a statement issued on its Amaq news channel.

The bombing came five days after ISIS-K claimed that it had orchestrated a bombing outside a Kabul mosque Sunday, killing at least two people, during a memorial service for the mother of the Taliban’s acting deputy information minister and main spokesman, Zabihullah Mujahid.

An explosion ripped through a mosque in northern Afghanistan Oct. 8, killing and wounding dozens of people at Friday prayers in Kunduz province. (The Washington Post)

ISIS-K has emerged as the biggest security threat since the Taliban seized control of Afghanistan in mid-August. In late August, ISIS-K claimed responsibility for a suicide bombing at Kabul’s international airport that killed 13 U.S. service members and some 170 Afghan civilians, capping the frantic end of America’s longest war after two decades in Afghanistan.

Since the U.S. withdrawal, the two groups have engaged in deadly tit-for-tat attacks in several parts of the country. ISIS-K has claimed that it was behind a series of blasts in the eastern city of Jalalabad targeting the Taliban. Afghanistan’s new rulers, in turn, have been going after suspected and former ISIS-K members in a spate of reprisal killings.

On Friday, some Hazara residents of Kunduz said they fear they have now become a part of this deadly contest for power and authority in the new Afghanistan.

“The Shia people are stuck in a war between the Taliban and the Daesh,” said a Hazara community leader in Kunduz, using the Arabic acronym for the Islamic State, and speaking on the condition of anonymity to talk freely without reprisals from either side.

The Taliban on Friday sought to defuse the tensions, calling Hazaras comrades.

“This afternoon, an explosion took place in a mosque of our Shiite compatriots … a number of our compatriots were martyred and wounded,” Mujahid, the spokesman, said in a tweet.

The Taliban, he added, had deployed “a special unit” to investigate the bombing.

The United Nations mission in Afghanistan condemned Friday’s attack in a tweet, saying initial reports indicated “more than 100 people killed and injured in a suicide blast inside the mosque.” In a second tweet, the U.N. said, “Today’s incident is part of a disturbing pattern of violence: 3rd deadly attack this week apparently targeting a religious institution.”

In addition to the Kabul mosque bombing, there was an attack on a religious school in Khost province that is so far unclaimed, said the U.N.

Some accounts placed the number of dead as high as 100, but hospital officials, community leaders and witnesses said at least 50 were killed, though the death toll was expected to rise. The Doctors Without Borders hospital alone received 21 bodies and 99 wounded, all of them men.

Many had serious burn injuries and loss of limbs, and were in need of urgent surgery, said Esmatullah Esmat, the deputy for medical activities at the charity. “Most had multiple injuries,” he said. “This was a bomb blast where we can find 20 wounds in a single patient.”

Friday’s blast unfolded around 1:15 p.m., when more than 300 worshipers were gathered inside the Sayed Abad mosque for Friday prayers. The men sat on the ground floor; the women on the floor above. The mosque was so packed that an overflow of worshipers prayed outside, witnesses said.

The huge explosion occurred during the first part of the prayer, “followed by fire and thick smoke,” said Niazi, a neighborhood resident who was praying outside. Survivors with minor injuries straggled out with blood splattered on their clothes. When the fire died down, he entered the mosque, along with scores of other rescuers, trying to avoid stepping on pieces of bodies shredded by the blast.

“I couldn’t control my emotions to see the dead bodies and injured people crying for help,” said Niazi, who spoke on the condition that only one of his names be used because he feared for his safety. “There were no ambulances, no rescue officials. We started shifting the wounded people in private cars. Everyone was crying, screaming and yelling, no one was in control of their feelings.”

Farzana Mohseni, a 25-year-old resident of the Khanabad neighborhood, said almost every family in the area had lost one or two family members to the explosion.

“People can hardly identify the dead bodies because they are badly burned,” said Mohseni, whose father survived the bombing and was praying at the mosque. “People cannot find their family members.”

Ambulances arrived 20 minutes later, along with a contingent of Taliban fighters. The militants blocked off the area and started to help take the injured to hospitals around the city, said Niazi.

After the Islamic State claimed responsibility for the attack, Mujahid described it as “the act of miscreants,” and vowed to investigate and “take stern actions against the perpetrators.” Karimi, his deputy, said Friday night that Taliban fighters had launched an operation and that “a hideout of the perpetrators was fully eliminated.”

Some members of the Hazara community partly blamed the Taliban for the bombing. They said that for years they had their own security guards at the mosque, searching worshipers and keeping close watch for potential dangers. But after the Taliban took control of Kunduz, it took the guards’ weapons away. On Friday, no one was patting down people as they entered the mosque, community leaders said.

“The Taliban told us their fighters will now provide security,” the Hazara elder said. “Why was there no proper security arrangement at the mosque when the Taliban promised us?”

“People are very angry,” he added.

He urged the Taliban to allow Hazaras to carry weapons again for their own security.

“Such attacks will continue and will rise,” he said.

Mohammadullah Aryen in Kabul and Haq Nawaz Khan in Peshawar, Pakistan, contributed to this report.