Isis has claimed responsibility for an attack on a mosque in Kabul that left at least 30 dead.

The group's propaganda agency, Amaq, released a statement shortly after the attack saying its fighters were behind the killings.

Gunmen stormed the Shia mosque, attacking worshippers gathered for Friday prayers.

A cleric who was leading the service was among those killed, police said.

The Amaq statement referred to an "inghimasi" attack, where assailants use weapons to inflict the maximum number of casualties, only detonating their suicide vests when they have run out of ammunition.

As the attack unfolded, police encircled the mosque but initially avoided advancing inside to prevent further casualties, police official Mohammed Jamil said.

Later, officers attempted to enter the mosque but withdrew after one of the attackers set off an explosion.

By Friday evening police said they had secured the mosque, and all three attackers were dead.

Witnesses said the assailants threw grenades, and police officials said a suicide bomber had detonated himself at the gate, Reuters reported.

A second suicide bomber detonated among a group of women in the mosque, an official said.

Security sources put the overall toll at 30 people killed and "dozens" wounded.

Kabul suicide bombing kills at least 24 and leaves 40 injured

Mir Hussain Nasiri, a member of Afghanistan's Shia clerical council, said the gunmen had taken over the portion of the mosque with separate prayer areas for men and women.

He said the mosque could accommodate up to 1,000 people. Access to the second floor, where the women pray, was blocked by attackers. He said that meant they gunmen were holding women who were there as hostages.

Police trying to evacuate worshippers from the mosque (Omar Sobhani/Reuters)

Witnesses in the area said sporadic shooting inside the mosque continued throughout the day.

There have been several attacks against Shia Muslim mosques in Afghanistan in recent weeks.

Last month, 32 people were killed when gunmen stormed a mosque in western Herat province.

Isis also claimed responsibility for that attack and vowed to carry out more attacks against Afghanistan's minority Shia, which the Sunni extremists consider to be heretics.