Gunshots and explosions could be heard and witnesses reported seeing worshippers smash windows to escape the building.
Gunshots and explosions could be heard and witnesses reported seeing worshippers smash windows to escape the building, which was believed to have been packed with dozens of men, women and children when the assailants struck during Friday prayers.
Heavily-armed security forces have surrounded the mosque in a residential area in the north of the city, and tempers quickly frayed as onlookers called for them to storm the building.
Kabul police spokesman Abdul Basir Mujahid told AFP a suicide bomber "detonated himself inside the mosque" and a health ministry official said at least two people had died while 11 were wounded, without specifying if they were civilians.
An interior ministry spokesman told media that at least two police officers had been killed.
People gathered outside the mosque were trying to call women and children trapped inside but their mobile phones were not responding, an AFP reporter at the scene said.
"Our relatives have been stranded inside the mosque... We believe they have been held hostage by the gunmen. We are very concerned about their safety and may God help us and rescue our loved ones," one of the onlookers said.
The attackers were holed up in the women's section, Sayed Jan Agha, who works in the mosque and whose mother is one of those trapped inside, told AFP.
"The bombers are running short of bullet rounds and they are using knives to stab worshippers," another eyewitness said.
Witnesses said the attackers were also armed with rocket propelled grenade launchers.
The assault underscores spiralling insecurity in Afghanistan as a resurgent Taliban steps up offensives across the country, while the Sunni Islamic State group, known for carrying out sectarian attacks, expands its Afghan footprint.
It comes just days after US President Donald Trump cleared the way for thousands more American troops to be deployed in the war-torn country.
Najib Danish, a deputy spokesman for the interior ministry, told TOLOnews that initial information suggested a suicide bomber detonated himself and two or three other militants had entered the mosque and were exchanging fire with police.
"Two Afghan police forces were martyred and two others were wounded and have been taken to hospital. All the four police officers were responsible for the security of the site," Danish wrote on Facebook.
More than 10 ambulances were at the scene to take the wounded to local hospitals. Some relatives flocked to a nearby private hospital to search for loved ones who had been in the mosque at the time of the attack.
Pools of blood could be seen at the entrance to the medical facility.
There was no immediate claim of responsibility but Shiites, a minority of around three million in overwhelmingly Sunni Afghanistan, have regularly been targeted by IS jihadists recently. They accuse police and troops of failing to protect them.
IS has claimed responsibility for a series of attacks killing dozens of Shiites in Kabul over the past year, including twin explosions in July 2016 that ripped through crowds of Shiite Hazaras, killing at least 85 people and wounding more than 400.
Earlier this month 33 worshippers were killed and 66 wounded in a suicide attack claimed by IS on a Shiite mosque in the western Afghan city of Herat.