KABUL — An Islamic State gunman stormed a temple used by Sikh and Hindu minorities in Kabul on Wednesday, killing 25 worshipers during an hours-long battle with Afghan security forces.

More than six hours after the attack began, the Interior Ministry said the building had been cleared. Spokesman Tariq Aryan said eight were wounded in addition to the 25 killed. Afghan security forces rescued 80 others from the site, he said.

The Islamic State asserted responsibility for the attack in a statement posted by the group’s media arm, according to SITE Intelligence Group, which tracks the online postings of Islamist militant organizations.

Although Afghan President Ashraf Ghani declared that the Islamic State was defeated late last year, officials estimated Islamic State fighters scattered across the country number in the hundreds. The group’s supply and recruitment networks were also believed to be badly damaged, and for months last year the extremists were unable to carry out attacks in Kabul.

The Taliban denied any involvement in the attack Wednesday. After signing a peace deal with the United States last month, the group has been under pressure to reduce violence and enter into talks with the Afghan government. But the Islamic State is allied with neither the government nor the Taliban and has continued its attacks.

The lone gunman began firing on worshipers during a Wednesday morning ceremony at the temple in a congested part of the Afghan capital. Aryan, the Interior Ministry spokesman, said the shooting was the first such attack on a house of worship used by Sikh and Hindu minorities in Kabul.

The sound of at least one blast was heard from the area during the attack, but it was unclear where it occurred and whether it was linked to the attack.

Anarkali Honaryar, an Afghan Sikh lawmaker, told The Washington Post by phone that children were among the casualties and that it took security forces hours to secure the site because of the large number of civilians present.

Several ambulances were seen rushing to the site and evacuating the wounded. U.S.-led coalition troops were also dispatched to the scene to support Afghan security forces.

A coalition defense official said the response to Wednesday’s attack was “Afghan-led and Afghan-executed, with some advice and assistance from Resolute Support,” the U.S.-led military command in Kabul.

The official added that the U.S.-led coalition “is committed to our ongoing support for Afghan security forces.” The official spoke on the condition of anonymity because he was not authorized to release details of the incident for attribution.

The United States set a timetable for the withdrawal of all U.S. forces from Afghanistan in the peace deal signed with the Taliban in February. The absence of U.S. forces in Afghanistan is expected to put increased pressure on Afghan security forces.

“The attack on the religious sites shows the extreme weakness of the enemy,” Ghani said in a statement. “Religious sites should not be vulnerable to attacks and violence.”

The U.S. Embassy in Kabul condemned the attack in a tweet.

The Islamic State also asserted responsibility for an attack this month that killed dozens of civilians gathered at a commemoration ceremony for a slain leader of the Hazara ethnic group, who are largely followers of the Shiite sect of Islam.

Sikhs and Hindus, religious minorities in Afghanistan, have suffered immensely from persecution during the decades of war in the country. Many of them have left Afghanistan because of war and rising crime.

Prominent Sikh leaders were also killed in an Islamic State suicide attack in 2018 in the eastern city of Jalalabad.

Sharif Hassan in Kabul and Susannah George in London contributed to this report.