The blast occurred when people were leaving the mosque at Pat Bazar in Hangu town of Khyber-Pakhtunkhwa province.
The bomb went off shortly after Friday prayers had ended.
Hangu district police chief Mian Mohammad Saeed told reporters that an improvised explosive device had been planted near the mosque.
"Twelve people were killed and 20 others were injured," he said.
No group has claimed responsibility for the attack.
Hangu has witnessed several terror attacks in the past few years.