A suicide bomb at a Shia mosque in Kabul killed more than 30 people and wounded dozens as worshippers gathered for a religious ceremony, officials said.

The attacker was among worshippers inside the mosque when he detonated the bomb, a senior police official said, giving the toll.

A government spokesman said the blast occurred around 12.30pm local time.

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

Police cordoned off the area around the Baqirul Olum mosque in the west of the Afghan capital as ambulances rushed to the scene.

"I was in the mosque, the people were offering prayers. Suddenly I heard a bang and windows broke. I had no idea what had happened. I rushed out screaming," a witness told AFP.

President Ashraf Ghani said in a statement he strongly condemned the "barbaric" attack.

Mosques in Kabul generally do not have any security - though police patrols can be seen near some on Fridays.

Worshippers were gathering for the major Shia ceremony of Arbaeen, marking the end of a 40-day mourning period after Ashura, which commemorates the death of Imam Hussein.

The pilgrimage is seen as a major potential target for sectarian attacks.

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

It followed twin attacks claimed by the so-called Islamic State group, that also targeted Shias and killed 18 in Kabul.

In July, IS jihadists 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 Afghan capital since the Taliban were ousted from power in a 2001 US-led invasion.