The attack in the town of Maymana, capital of northern Faryab province, came as people were gathering at the mosque to celebrate the Eid al-Adha holiday.
Top provincial officials, including the governor and the police chief, were inside the building when the bomber set off his explosives outside, where a large crowd had gathered, officials said. The officials were not hurt, but most of the dead were police officers and soldiers.
"The targets of the bomber were all the officials inside the mosque," Deputy Governor Abdul Satar Barez said. He said the dead included 14 civilians.
"There was blood and dead bodies everywhere," said Khaled, a doctor who was in the mosque at the time of the blast. "It was a massacre," said Khaled, who like many Afghans uses only one name.
Video from the scene showed the motionless bodies of several soldiers and policemen lying next to their vehicles parked on a tree-lined avenue of the city, located about 500 kilometers (300 miles) northwest of Kabul. On the sidewalk, a number of civilians lay along the mosque's outer wall, some writhing and moaning in pain.
It appeared to be the deadliest suicide attack in recent months.
On Sept. 4, 25 civilians were killed and more than 35 wounded in Nanghar province, and on Sept. 1, 12 people were killed and 47 wounded in a suicide attack in Wardak province.
Afghan President Hamid Karzai strongly condemned the attack, saying that those who carried it out were "enemies of Islam and humanity."
He said in a statement that 36 people died in the blast and 23 were injured.
The attack came as Karzai was urging Taliban insurgents "to stop killing other Afghans."
In his Eid al-Adha message to the nation on Friday morning, Karzai called on the insurgents to "stop the destruction of our mosques, hospitals and schools."
The United Nations says that Taliban attacks account for the vast majority of civilian casualties in the 11-year war. The insurgents routinely deny that they are responsible for attacks on civilians, saying they target only foreign troops or members of the Afghan security forces.
On Wednesday, Taliban leader Mullah Mohammad Omar urged his fighters to "pay full attention to the prevention of civilian casualties," saying the enemy was trying to blame them on the insurgents.
Also Friday, the Taliban claimed responsibility for killing two American service members in southern Uruzgan province, in what may have been the latest insider attack against Western troops.
In an emailed statement, Taliban spokesman Yusuf Ahmadi said a member of the Afghan security forces shot the two men the day before, then escaped to join the insurgents.
A spate of insider attacks has undermined trust between international troops and Afghan army and police, further weakened public support for the 11-year war in NATO countries and increased calls for earlier withdrawals.
Maj. Lori Hodge, spokeswoman for U.S. forces in Afghanistan, said on Thursday that authorities were trying to determine whether the latest attacker was a member of the Afghan security forces or an insurgent who donned a government uniform.
It was the second suspected insider attack in two days. On Wednesday, two British troops and an Afghan policeman were gunned down in Helmand province.
Before Thursday's assault, 53 foreigners attached to the U.S.-led coalition had been killed in attacks by Afghan soldiers or police this year.