Six children and two adults became trapped earlier on Tuesday when a cable snapped while they were crossing a river canyon.
The six children and two adults became trapped around 7am (02:00 GMT) when a cable snapped while they were crossing a river canyon in the Battagram district in Khyber Pakhtunkhwa province.
Residents used mosque loudspeakers to alert neighbourhood officials of the emergency and hundreds of people gathered on both sides of the ravine – hours away from any sizeable town – to watch the drama unfold.
The daring rescue began with a helicopter plucking two children to safety after almost 12 hours in the air as daylight faded, but the chopper was forced back to base in the dark.
Rescuers then used the cable keeping the gondola from plunging into the valley as a zipline to rescue the rest of those stranded late into Tuesday night.
“Once everyone had been rescued, the families started crying with joy and hugging each other,” emergency official Waqar Ahmad told the AFP news agency.
“People had been constantly praying because there was a fear that the rope might break. People kept praying until the last person was rescued.”
Al Jazeera’s Kamal Hyder speaking from Islamabad said the operation required a “massive effort” and there was “sigh of relief across the country”.
“It involved not just the military but the local expertise, including brave volunteers from the area, as well,” he added.
In a statement, the military said the rescue operation was delicate and involved the commandos, pilots from the army and air force and the support of local authorities.
Some of those trapped were in contact with their families via mobile phones.
Bilal Faizi, an official with Pakistan’s emergency service, said the two adults were the last to be brought to safety.
A video of the first rescue showed a teenager in a harness hanging at the bottom of a swinging rope under a helicopter as crowds cheered with relief.
Earlier in the day, as the rescue operation unfolded, headmaster Ali Asghar Khan said the children were teenage boys and students at his government high school Battangi Pashto.
“The school is located in a mountainous area and there are no safe crossings, so it’s common to use the chairlift,” Khan said.
“The parents are gathered at the site of the chairlift. What can they do? They are waiting for the rescue officials to get their children out. We are all worried.”
Abid Ur Rehman, a teacher from another school in the area, said about 500 people had gathered to watch the rescue mission.
“Parents and women are crying for the safety of their children,” he told AFP.
Villagers frequently use cable cars to get around Pakistan’s mountainous regions.
But the cars are often poorly maintained, and every year, people die or are injured while travelling in them.
President Arif Alvi said called for authorities to conduct urgently a “comprehensive survey” of all cable car lifts in the country.