ISLAMABAD — An American woman, her Canadian husband and their three young children have been released after years of being held captive by a network with ties to the Taliban, US and Pakistani officials said Thursday.

US officials say Pakistan secured the release of Caitlan Coleman of Stewartstown, Pennsylvania, and her husband, Canadian Joshua Boyle, who were abducted five years ago while traveling in Afghanistan and had been held by the Haqqani network in Pakistan.

Coleman was pregnant when she was captured. The couple had three children while in captivity, and all have been freed, US officials say.

A US national security official, speaking on condition of anonymity to discuss an ongoing operation, commended Pakistan for their critical assistance in securing the family’s release — and described the cooperation as an important step in the right direction for US-Pakistani relations.

The US has long criticized Pakistan for failing to aggressively go after the Haqqanis, who have been behind many attacks against US and allied forces in Afghanistan.

In Pakistan, its military said in a statement that US intelligence agencies had been tracking the hostages and discovered they had come into Pakistan on Oct. 11 through its tribal areas bordering Afghanistan.

“All hostages were recovered safe and sound and are being repatriated to the country of their origin,” the military said.

Three Pakistani military officials, all speaking on condition of anonymity as they weren’t allowed to speak to journalists, also confirmed the hostages’ identities.

The release, which came together rapidly Wednesday, comes nearly five years to the day since Coleman and Boyle lost touch with their families while traveling in a mountainous region near the Afghan capital, Kabul.

The couple set off in the summer 2012 for a journey that took them to Russia, the central Asian countries of Kazakhstan, Tajikistan and Kyrgyzstan, and then to Afghanistan. Coleman’s parents last heard from their son-in-law on Oct. 8, 2012, from an internet cafe in what Boyle described as an “unsafe” part of Afghanistan.

In 2013, the couple appeared in two videos asking the US government to free them from the Taliban.

Coleman’s parents, Jim and Lyn Coleman, told the online Circa News service in July 2016 that they received a letter from their daughter in November 2015, in which she wrote that she’d given birth to a second child in captivity. It’s unclear whether they knew she’d had a third.

“I pray to hear from you again, to hear how everybody is doing,” the letter said.

In that interview, Jim Coleman issued a plea to top Taliban commanders to be “kind and merciful” and let the couple go.

“As a man, father and now grandfather, I am asking you to show mercy and release my daughter, her husband, and our beautiful grandchildren,” Jim Coleman said. “Please grant them an opportunity to continue their lives with us, and bring peace to their families.”

The family was being held by the Haqqani network. US officials call the group a terrorist organization and have targeted its leaders with drone strikes. But the group also operates like a criminal network. Unlike the Islamic State group, it does not typically execute Western hostages, preferring to ransom them for cash.

President Trump has called on Pakistan to do more to tackle militant organizations that use its territory as a home base.

“We can no longer be silent about Pakistan’s safe havens for terrorist organizations, the Taliban and other groups that pose a threat to the region and beyond,” Trump said in a recent speech announcing his Afghanistan policy. He issued a stark warning: “We have been paying Pakistan billions and billions of dollars at the same time they are housing the very terrorists that we are fighting. But that will have to change, and that will change immediately.”