Bodies were seen scattered inside the Said Abad mosque, used by the minority Shia Muslim community.
More than 100 people were injured in the blast in the northern city.
The Islamic State group said it was behind the attack. Sunni Muslim extremists have targeted Shias who they see as heretics.
IS-K, the Afghan regional affiliate of the IS group that is violently opposed to the governing Taliban, has carried out several bombings recently, largely in the east of the country.