An explosion at a mosque near police offices in northwestern Pakistan has killed at least 21 people and wounding at least 80, authorities say.