No one claimed responsibility for the attack, but a local government official blamed the Taliban and said it was probably retaliation for a Pakistani military offensive against militants in the Swat Valley region.
It was unclear whether any military figures or prominent anti-Taliban local officials were in attendance at the Sunni Muslim mosque in the village of Haya Gai in Upper Dir, a rough and tumble district next to Swat. The village is about 65 miles (100 kilometer) north of Peshawar, about 30 miles (50 kilometers) northwest of Mingora, the largest city in the Swat Valley.
The Taliban has threatened a campaign of revenge attacks for the offensive. Although most of the bombings have targeted security forces, militants have also targeted civilians -- most recently, a marketplace blast in Peshawar that killed six civilians.
The motive for such attacks on civilians is rarely clear, but it could be partly an attempt to use violence and intimidation to weaken public support for the army's operation.
Police said a man wearing an explosives vest entered the mosque but was recognized as a stranger by some worshippers. When they confronted the man, he blew himself up, said Atlass Khan, an Upper Dir police official.
"People tried to intercept him because he looked like an outsider, someone who does not belong to this area," Khan told The Associated Press by phone.
Militants in Pakistan and Afghanistan have often killed scores of civilians in attacks directed anywhere that people gather, including mosques and funerals. On March 27, a suicide bomber blew up a packed mosque near the Afghan border during Friday prayers, killing 48 people and wounding scores more.
Waliullah Khan, a village resident, said he was on his way to the mosque when he heard an explosion.
"I rushed there and saw smoke and dust," said Khan, who helped transport wounded people to the hospital. "Human body parts were lying there, there was blood and people were crying in pain. I counted at least 15 bodies."
Police Chief Ejaz Ahmad said the confirmed death toll was 30, but the number was expected to increase because there were more body parts to be counted and some of the 40 wounded were in critical condition.
Atif-ur-Rehman, a top official in Upper Dir's government, blamed the Taliban, though he said the investigation was in its early stages.
"It is obvious. They are Taliban," he told AP. "We can say it seems to be a reaction to the offensive in Swat."
Pakistan launched its Swat offensive in late April, after the Taliban violated a peace deal with the government that gave them control of the valley by advancing into nearby Buner district just 60 miles (100 kilometers) from Islamabad, the capital.
Washington strongly backs the operation and sees it as a test of Pakistan's resolve to beat al-Qaida and Taliban militants implicated in attacks on Western forces in neighboring Afghanistan.
But the generally broad public support in Pakistan for the operation could falter if militant violence widens or if the government fails to successfully resettle some 3 million refugees from the fighting.
Richard Holbrooke, the U.S. special envoy to Pakistan and Afghanistan, said Friday the offensive appeared to be clearing Swat of militants, though handling the refugee crisis would be "the real test" of success.
Interior Minister Rehman Malik said the situation in Swat would improve in the next few months, but in the meantime, it was better for residents to stay away.
"Obviously, there was a war in that area, there is no electricity, there is a problem of water, structure of other civic facilities are destroyed," Malik told reporters in Islamabad. The military said this week that major population centers and roads leading to the valley were rid of Taliban resistance but isolated violence would likely continue for some time.
Many of civilians displaced from the Swat offensive have been impatient to return home, and some are becoming frustrated.
"I want nothing from the government. I only want that we should be allowed to go back to our Mingora city," said Dilawar Khan, 40, who left a refugee camp with his two wives and four children Friday when he heard a curfew preventing travel was going to be lifted.
Instead, they were blocked by soldiers at nearby Got Koto.
"We can no longer sit at the camps where there is only dust, diseases and heat," said Zubayda Bibi, one of the wives. Even if damaged, "home is better than anything."