Ajab Khan, a police officer, said 20 people were killed and 34 wounded, mostly soldiers.
Some bystanders in shops and restaurants along the road were also hit.
State television reported that shooting started after the explosion, the latest in a series of attacks on security forces in recent months which have killed hundreds of people.
It also announced that Pervez Musharraf, Pakistan's president, had condemned the attack.
Taj Mohammed Khan, 23, a college student who was drinking tea at a nearby roadside restaurant, said: "It was a huge explosion. Then the truck was on fire. There were flames, smoke and people crying. People were scared to go near because bullets were going off."
Saeed Khan, a resident at the scene, said: "When we reached near the truck it was burning. Flames were rising high into sky. Ammunition was exploding. Police stopped us going near the truck."
Tribal fighters
Pakistani tribal areas have provided shelter to al-Qaeda and pro-Taliban fighters who have fled Afghanistan.
Violence has escalated across Pakistan since July, when fighters abandoned a peace deal and the army raided a mosque in the capital, Islamabad.
Tribal fighters have also been blamed for carrying out the worst attack in Pakistan's history last week.
At least 139 people were killed in a suicide attack on a procession led by Benazir Bhutto, a former prime minister, in Karachi last week on her return from eight years of self-imposed exile.