Two of the assailants blew themselves up and another two were shot to death by Afghan security forces, according to police official Mohammed Sadique Muradi.
The Islamic State group claimed responsibility for the attack, the latest to target Afghanistan's minority Shiites.
The Taliban condemned the violence, with a spokesman for the militants, Zabihullah Mujahid, telling The Associated Press in a telephone interview that the group had nothing to do with it.
The death toll of 20 was expected to rise because many of the victims were seriously wounded, said Mohammad Salim Rasouli, chief of Kabul's hospitals.
More From This Section
Terrified worshippers endured about four harrowing hours of gunfire and explosions during the afternoon before the four attackers were killed.
The Islamic State said in a statement on the website of its Aamaq news agency that it had deployed two attackers to the mosque. There was no immediate explanation for the contradictory number of attackers.
Later, as police tried to advance, one of the attackers set off an explosion that forced them to withdraw, Muradi said.
The cleric who was performing the prayers was among the dead, said Mir Hussain Nasiri, a member of Afghanistan's Shiite clerical council. The gunmen had taken over both the cavernous prayer hall for the men and the separate, second- floor prayer area for the women, he said.
The mosque could accommodate up to 1,000 people, Nasir added.
"I was trying to escape over the wall when I saw my daughter, who was wounded, also trying to climb the wall," one man who gave his name only as Bismillah told the AP.
"There was another girl who was shot in the head. I saw the body myself," he said. "Finally I managed to escape with my daughter and a police escorted us to safety from the back of the mosque."
Within days of that, IS also took responsibility for a suicide attack on a Shiite mosque in western Herat province that left 32 people dead.
In southern Kandahar province Friday, Afghan security forces repulsed a Taliban attack on an outpost overnight, according to provincial police chief's spokesman, Zia Durrani.
Four members of the security forces died in the exchange and another seven were wounded, he said.
Durrani said the Taliban sustained heavy casualties. There was no immediate comment from the militants.
The summer fighting season in Afghanistan has seen relentless Taliban attacks as the insurgents battle to expand their footprint.
Yesterday, Gen John Nicholson, the top US general in Afghanistan, and Hugo Llorens, the US Embassy's special charg d'affaires, told reporters in Kabul that the new US strategy was a promise to Afghans that together they would defeat terrorism and prevent terrorist groups from establishing safe havens.
Nicholson vowed to defeat both the Islamic State group affiliate and the remnants of al-Qaida, and he had the following message to the Taliban: "Stop fighting against your countrymen.
President Donald Trump had announced the new plans for Afghanistan on Monday. While he did not give specifics, senior US officials have said that he might send up to 3,900 more troops, with some deployments beginning almost immediately.