There were conflicting accounts about the identity of the bomber. The army and police said he was a teenager in a school uniform, but the Pakistani Taliban claimed he was a soldier at the camp in Mardan town who volunteered for the attack.
The bombing showed that despite years of army operations against their hideouts along Pakistan's border with Afghanistan, Taliban and al-Qaida-linked fighters retain the ability to strike back. It was one of the worst attacks on security forces in recent months.
Senior police official Samad Khan said 31 soldiers died and 42 were wounded, some critically. The army, which tends to release information much slower, put the death toll in an earlier statement at 20. All of those killed were cadets except for one, who was a drill instructor, said the army.
An examination of the body parts at the scene indicated the bomber was a teenage boy, which is a common finding in suicide bombings in Pakistan, said Khan. The army also said he was a teenager in a school uniform. The training camp area holds several army-run schools.
Taliban spokesman Ahsanullah Ahsan disputed the government's account in a phone call to The Associated Press, saying the bomber was a soldier in Mardan who approached them and said "he wanted to sacrifice his life for Islam."
"We accepted his offer and told him to target his fellow soldiers in Mardan," said Ahsan.
Former army soldiers have been suspected in attacks in Pakistan, but a suicide bombing by an active duty soldier would be rare, if not unheard of.
U.N. Secretary-General Ban Ki-moon condemned the suicide bombing and was appalled by the report that a child might have been used to commit the act, his spokesman Martin Nesirky said. Ban extended his condolences to the families of the victims and to the government of Pakistan, the spokesman added.
Troops quickly cordoned off the area after the bombing and even police had a difficult time getting through. The same training facility was attacked by a suicide bomber in 2006 who killed 35 soldiers.
The army has staged multiple offensives in Pakistan's northwest aimed at taking out the Pakistani Taliban in recent years. Its efforts against the group, which is distinct from but linked to the Afghan Taliban, appear to have been largely successful - but violence persists.
The U.S. has encouraged Pakistan to eliminate the Pakistani Taliban in the belief that the long-term stability of the nuclear-armed Muslim nation is critical to global security.
Washington also wants Islamabad to take out militants who focus on fighting the U.S. and NATO in Afghanistan but who have bases on Pakistani soil, in particular in the North Waziristan tribal region. But Pakistan has yet to mount an offensive in that area.
In North Waziristan on Thursday, the bullet-riddled bodies of two tribal police officials and a villager were discovered along an open road near the town of Mir Ali, a militant stronghold. A note attacked to the bodies accused them of acting as spies for the U.S., resident Asif Iqbal said.
Intelligence officials said the three men had been kidnapped in January. Their bodies showed signs of torture, the officials said, speaking on condition of anonymity because they were not authorized to speak to media.
Militants have killed numerous North Waziristan residents after accusing them of spying. The killings are often linked to allegations that local residents are providing intelligence on militants that helps the U.S. launch missile strikes in the area.
Despite the danger from Islamist militants, the Pakistani army's main focus continues to be the perceived threat from its archrival and nuclear-armed neighbor, India.
Pakistan successfully test-fired a cruise missile capable of carrying "strategic and conventional war heads" from an undisclosed location Thursday, said the army. It didn't specify whether the missile - identified as a Hatf-VII Babur, with a range of 360 miles (600 kilometers) - could carry a nuclear warhead.
Pakistan and India have fought three wars since they gained independence from Britain in 1947 and routinely test different versions of their missiles, partly as a show of force.
Associated Press writers Rasool Dawar in Peshawar, Pakistan, and Anita Snow at the United Nations in New York contributed to this report.