Television footage showed a gaping hole in the rough mud wall around the school near the southwestern city of Quetta and one partly demolished adjacent room.
Police said Friday's blast occurred in the wrecked room but didn't indicate if it was an attack or if the bomb was being kept there. One witness said it was detonated by a suicide bomber.
Quetta, the capital of Baluchistan province, has a rich cast of violent groups.
The city is considered a hub for Taliban militants fighting in neighboring Afghanistan. It has a history of sectarian violence. The province is also the scene of a low-level insurgency waged by ethnic Baluch nationalists seeking more autonomy.
Quetta police chief Wazir Khan Nasir said investigators were still trying to work out what kind of bomb went off and were questioning students and staff at the school.
He said the school was run by Jamiat Ulema-e-Islam, a hard-line Islamist party that is part of Pakistan's ruling coalition government, and had not been registered with authorities.
However, a local party leader denied any links to the school and suggested the incident was staged to discredit religious learning centers, known as madrassas.
"We regret the death of the students who were martyred in this incident," Hafiz Hamdullah told The Associated Press. "We fear that somebody is planning a crackdown against madrassas."
At a city hospital, a man who identified himself as Shahbaz Ahmad, told reporters he had seen students scuffling with a man who tried to push past them after they asked him why he wanted to enter the compound.
"When they barred his way, he blew himself up," said Ahmad, a young man with a black beard.
Later Friday, unknown gunmen riding a motorbike opened fire on a police patrol vehicle in Quetta, killing one officer and wounding one policeman and a passer-by, police official Raja Ishtiaq said.
It was unclear if the two incidents were related.
Pakistani officials have warned that militancy could heat up following a wave of cross-border strikes on militant bases by U.S. forces in Afghanistan.