A suicide bomber blew himself up at the entrance to a Shi'ite Muslim mosque in Kabul as other attackers stormed the building, killing at least 30 people including worshippers gathering for Friday prayers, officials said.
Islamic State, which has launched several attacks against minority Shi'ite targets in Afghanistan, claimed responsibility, the jihadist group's news agency said.
The assault sparked chaos as worshippers fled and others frantically searched for missing family members.
"The attackers are slaughtering people like sheep but there's no one to go and rescue them," said Murtaza, a young boy whose parents were trapped inside as the attack unfolded.
"A lot of people are on the ground and no one is trying to rescue them."
By Friday evening police said they had secured the mosque in the Khair Khana area of the capital, and all three attackers were dead.
Witnesses said they had thrown grenades, and police officials said a suicide bomber detonated himself at the gate.
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.
At least 10 civilians were killed, including women and children, while another 30 were wounded, Ministry of Interior spokesman Najib Danish said earlier in the day. At least three policemen were also killed and eight wounded, he said.
Police said they rescued more than 100 worshippers.
At least 15 of the wounded were taken to city hospitals, said Ismail Kawosi, a spokesman for the Ministry of Public Health.
One witness, Sayed Pacha, said four attackers had entered the mosque.
"At first a suicide bomber opened fire and martyred two security guards at the entrance of the mosque and then they entered inside," he told Reuters.
Human rights activists condemned the attack, the latest in a campaign of sectarian violence.
"Insurgents who carry out atrocities against a specific ethnic or religious community are committing war crimes and possibly crimes against humanity," Patricia Gossman, a senior researcher for Human Rights Watch, said in a statement.