A suicide bomber has blown himself up inside a Shia mosque in the Afghan capital Kabul, killing at least 27 people and wounding dozens of others.

The explosion happened at the Baqir-ul-uloom mosque in the Darul Aman area as people gathered to mark the end of an important religious period.

Kabul police chief Abdul Rahman Rahimi told Al Jazeera 27 people were killed and many others wounded, and that the death toll was likely to rise.

Fraidoon Obaidi, chief of the Kabul police Criminal Investigation Department, said the bomber mingled among the crowd on the first floor of the two-story mosque where he detonated his explosives.

“I heard a blast and dust covered the whole mosque,” worshipper Nadir Ali told AFP news agency.

“When the dust settled down, I saw the mosque was full of flesh and blood. I was injured in my waist and had to crawl out of the mosque.”

The United Nations said in a statement at least 32 had been killed and more than 50 wounded, including many children. It described the attack as “an atrocity”.

President Ashraf Ghani in a statement condemned the “barbaric” attack.

Several police vehicles raced from the scene and ferried the wounded to hospital.

READ MORE: Iran ‘foreign legion’ leans on Afghan Shia in Syria war

Worshippers were gathering to mark the Shia ceremony of Arbaeen, which comes 40 days after the day of mourning, Ashura.

Ashura commemorates the death of Imam Hussein, grandson of the Prophet Muhammad who was killed in battle in 680 AD. His fate laid the foundation for the faith practised by the Shia community, a minority in mainly Sunni-Muslim Afghanistan.

Arbaeen marks the end of the mourning period over his death.

The ISIL group claimed responsibility for the attack in a statement via its affiliated Amaq website, after the Taliban said it was not involved.

Earlier this year a powerful blast targeting Shia worshippers during Ashura killed 14 people in northern Afghanistan.

In July, ISIL group claimed responsibility for twin explosions that ripped through crowds of Shia Hazaras in Kabul, killing at least 85 people and wounding more than 400 others.

The bombings marked the deadliest single attack in the capital since the Taliban were ousted from power in the US-led invasion of 2001.

Shia Muslims in Afghanistan make up an estimated 15 percent of the country’s population of about 30 million.

Their public celebrations and commemorations were largely banned during the years when the Taliban controlled the country. But Afghanistan’s Shia community has become more public since the Taliban was ousted.

Critics said the government needs to do more to protect places of worship.

“Given the level of Shia-Sunni polarisation in the region, more tragic attacks of this sort are expected, while the Afghan government resorts only to verbal condemnation of such acts,” Al Jazeera’s Afghanistan analyst Hashmat Moslih said.

Amnesty International also took the government to task.

“[Afghan authorities] have a duty to take effective measures to protect Shia Muslims from attacks,” said Champa Patel, Amnesty’s South Asia director.