A young suicide bomber dressed in school uniform has blown himself up at an army compound in Pakistan, killing at least 31 people, officials say.

The boy attacked as recruits exercised at a Punjab Regiment parade ground, inside an enclosed military area of the north-western city of Mardan.

A Taliban militant said his group had carried out the bombing.

Such attacks on the army have decreased recently as militants have largely focused on civilian targets.

The BBC's M Ilyas Khan in Islamabad says the Taliban have long been trying to launch an attack in the Mardan cantonment - the city's enclosed military area.

He says the bomber managed to slip past at least six checkpoints by wearing the uniform of a college located inside the cantonment.

The exact age of the boy is unknown, but the military believe he was between 10 and 12 and his bomb was concealed under his school uniform.

Prime Minister Yusuf Raza Gilani condemned the attack.

"Such cowardly attacks cannot affect the morale of the security agencies and the resolve of the nation to eradicate terrorism," he said in a statement.

Mardan police official Abdullah Khan told the BBC that the teenage bomber had worn the uniform of the Aziz Bhatti College.

The bomber struck at 0800 (0300 GMT) as recruits were doing physical training on the parade ground, he said.

One resident, Abdul Wahab, described how he heard an explosion after his children had left for school.

"I rose in panic, I was worried about the children. I rushed out of my house and I saw ambulances coming and going. Then I came to know a blast had occurred inside this centre," he said.

Saif ur Rehman, 15, was at school in the cantonment at the time of the attack.

"Our Muslim brothers died in this attack, soldiers from our army. This was a loss for our nation, and for our religion," he told the BBC.

Taliban claims

The attack comes days after the authorities launched a major offensive against militants in the tribal region of Mohmand, near Mardan.

Thousands fled the area after the army used helicopter gunships and heavy weapons to pound suspected militant positions.

Mohmand, on the border with Afghanistan, has long served as a sanctuary for the Taliban and al-Qaeda.

Militants have increasingly been using teenage bombers across Pakistan.

Correspondents say Pakistani troops have uncovered camps in the north-west where children as young as 10 were being trained to become suicide attackers.

The Pakistani Taliban have attacked the country's security forces with devastating effect on a number of occasions in the past few years.

In perhaps the most brazen assault, militants dressed as soldiers attacked army HQ in Rawalpindi in October 2009, killing 19 people and taking dozens hostage.

But in recent months, militants have focused suicide attacks on civilian targets such as places of worship and markets.

Speaking to the AFP news agency from an undisclosed location, Taliban spokesman Azam Tariq said the organisation carried out Thursday's attack.

He said he was proud to avenge people killed by US drone attacks and by military operations in the tribal areas, and added that attacks would continue on "those who protect the Americans".

Pakistan's government supported the Taliban when the group was in power in Afghanistan from 1996-2001, but later became an ally of the US when it led an invasion in 2001.

Islamabad denies it is not doing enough to fight the militants, saying it has lost more than 2,400 troops in the war since 2002.