At least 31 people have been killed after a suicide bomber detonated a device at a Shia mosque, according to police.