The attack in the city of Peshawar also wounded more than 40 people, according to provincial information minister Mushtaq Ghani. There was intense shooting in the immediate aftermath of the first of two explosions, he said.
The Pakistani Taleban, who are fighting against the Islamabad government in a bid to establish a hardline Sunni theocracy, claimed responsibility.
Four or five attackers entered the mosque from a building being built next door then jumped over an outer wall into the mosque courtyard, police said. One of the attackers then detonated a suicide belt, killing himself and creating a diversion so that the other attackers could enter the mosque.
At least three attackers died in the violence – two by blowing themselves up, and a third was killed by worshippers.
“People here showed great courage. They grabbed one of the attackers [by] his neck, and he couldn’t detonate [his explosives], and he was shot and killed,” the police spokesman added. “Police have been called and an operation has started against the terrorists,” said Mian Saeed, police chief of the north-western city.
Radical Sunni Islamist groups often target mosques frequented by minority Shiites.
Some people who fled the mosque reported that at least some of the attackers were wearing security uniforms.
“The prayer was about to end when a big bang happened, followed by dust and smoke. I have seen amid the smoke that one guy dressed in police uniform was firing shots and then there was another blast,” said one of the wounded, 22-year-old Syed Javed Hasan, speaking from his hospital bed.
A spokesman for the Pakistani Taleban, Mohamad Khurasani, said the attack was in revenge for the execution of one of its members by the Pakistan government.
Witness Shahid Hussain said the worshippers had just finished prayers when five or six men wearing uniforms broke into the mosque and started shooting.
“We had no idea what was going on. One of the attackers then blew himself up and then there was huge smoke and dust all around,” he said.
The attack comes as Pakistan seeks to implement new measures to tackle militants following a massacre of 134 children at an army-run school in Peshawar.
The government has pledged to crack down on militant groups, and has reintroduced the death penalty, set up military courts to speed convictions and widened its military campaign in north-west areas on the Afghan border.