The firecracker exploded under the vehicle's fuel tank, setting it and the fireworks inside on fire in central Lahore, the capital of Punjab province, said Amir Zulfikar, a senior police officer.
"It took seconds until the bus was engulfed by flames and people hardly had any chance to rescue anyone," he said.
Forty bodies were pulled from the burned bus — including many burned beyond recognition — and 12 people were injured, Zulfikar said.
A survivor said the fireworks exploded with a bang that sounded like a bomb.
"It was smoke and dust everywhere after the explosion," said Naseem Khan, who jumped out of a broken window in the bus.
"Everyone was crying for help," Khan said from a hospital bed where he was being treated for burns and cuts.
Deaths from fireworks accidents are common in Pakistan and often occur at makeshift factories with poor safety conditions for storing and handling the explosive materials.