President Trump praised the Pakistanis for their role in freeing the family.
“This is a positive moment for our country’s relationship with Pakistan,” Mr. Trump said in a statement on Thursday. “The Pakistani government’s cooperation is a sign that it is honoring America’s wishes for it to do more to provide security in the region.”
The family was at the American Embassy in Islamabad late Thursday, Tariq Azim Khan, a Pakistani diplomat, said in a telephone interview from London. The Pakistani military pledged to repatriate them, and American officials were exploring how to get the family out of South Asia. Mr. Boyle’s relatives said they expected him to return home in the coming days.
“Josh indicated that they’d like to come back to Canada,” his mother, Linda, said outside of the family’s stone house in Smiths Falls, Ontario, about an hour southwest of Ottawa. “That was their plan right now.”
The return trip was complicated by Mr. Boyle’s refusal to board an American C-130 to take the family out of Pakistan and to Bagram Air Base in Afghanistan, where Americans have been accused of abusing detainees. His father said Mr. Boyle was philosophically opposed to traveling to the base.
After marrying in 2011, Ms. Coleman and Mr. Boyle spent months traveling in Central America before leaving for a trip through Russia and Central Asia. They had planned to leave Afghanistan in late 2012 because of Ms. Coleman’s pregnancy. But they were kidnapped in October of that year while backpacking in Wardak Province, a militant stronghold near Kabul.
In exchange for the family’s freedom, the Haqqani network had previously demanded the release of Anas Haqqani, one of its commanders. The Afghan government captured Mr. Haqqani in 2014, and he was sentenced to death. The militant group had threatened to kill the family if he was executed.
Earlier attempts to bring the family home fell short. In January 2016, Colin Rutherford, a Canadian, was freed after Qatar arranged a prisoner swap with the Afghan government. Officials had hoped Mr. Rutherford would be the first in a series of releases, including Ms. Coleman and her family.
But that never materialized, for reasons that remain unclear. No prisoners were exchanged and no money was paid to secure the family’s release, Mr. Khan said.
The Obama administration sought to jump-start talks with the Taliban but those efforts faltered after the American military killed Mullah Akhtar Muhammad Mansour, the Taliban’s leader, in a drone strike in May 2016.
Ms. Coleman’s pregnancies added pressure to resolve an already desperate situation, one in which the Haqqanis had repeatedly threatened to kill the family, including the children.
“The likelihood of a successful rescue was pretty much discounted in our minds,” said Patrick Boyle, Mr. Boyle’s father.
The plight of the family attracted widespread attention in Canada, though not all of it sympathetic.
Deeply interested in Islam and terrorism, Mr. Boyle was previously married to the oldest sister of Omar Khadr, a Canadian captured by American troops in Afghanistan in 2002 when he was 15 and held for a decade at the wartime prison at Guantánamo Bay, Cuba. He was repatriated in 2012.
Mr. Boyle volunteered as a spokesman for Mr. Khadr’s relatives, who did little to endear themselves to the Canadian public and domestic security services, according to Canadian media reports. Mr. Khadr’s father was killed in 2003 by Pakistani forces near the border with Afghanistan. Osama Bin Laden was a guest at a previous wedding of Mr. Khadr’s sister, Zaynab, who spoke publicly in support of terrorism and wed Mr. Boyle in 2009, a marriage that ended a year later.
In 2009, Mr. Boyle’s parents’ Ottawa home was burgled, though bullet holes in the home prompted the authorities to question whether the crime was linked to Mr. Boyle’s first wife or his father’s job as a federal tax judge.
The end of the family’s captivity was a victory for State Department officials and the F.B.I.-led Hostage Recovery Fusion Cell, which had worked on the case for years. The group was created under the Obama administration to improve the government’s efforts to free hostages and to ensure their families received timely information about them.
The Haqqanis are believed to still be holding an American university professor, Kevin King, who was kidnapped in August 2016. On a video released this year, Mr. King pleaded for Mr. Trump to free him: “Have mercy on me and get me out.”
Another American, Paul Overby, disappeared in May 2014 when he was trying to interview the leader of the Haqqani network.
The Trump administration has had modest success in freeing Americans held overseas. In April, an Egyptian-American woman was released in Egypt after being held for nearly three years on human trafficking charges. Another American, Otto Warmbier, a student, was released from North Korea but died days later. His family said Mr. Warmbier, who had been in a coma, was tortured. Three other Americans remain imprisoned in North Korea.
The C.I.A. director, Mike Pompeo, tried to open a backchannel to the Syrian government to help free Austin Tice, an American journalist and former Marine officer, who is being held in Syria. His exact whereabouts is unknown, but American officials believe the Syrian government knows where he is being held.
In an unusual moment on Wednesday night, Mr. Trump appeared to telegraph the family’s impending release.
“America is being respected again,” the president said during a speech on his tax plan in Harrisburg, Pa. “Something happened today where a country that totally disrespected us called with some very, very important news.”
Mr. Trump did not mention Ms. Coleman, Mr. Boyle or Pakistan, but the revelation was notable for a president who had often complained that President Barack Obama shared what Mr. Trump viewed as important secrets, such as battle plans against the Islamic State.
Pakistan’s military said it took custody of the family “through an intelligence-based operation.” American drones had tracked the vehicle carrying them to the outskirts of Kohat, a city in northwest Pakistan. It was the deepest that American drones have penetrated into Pakistan’s airspace, a Pakistani security official said.
Pakistan’s relationship with the United States has been rocky. The United States has long accused its military and its intelligence agency of harboring or ignoring militants, and relations have grown increasingly strained over Pakistan’s role in Afghanistan.
In a statement about the release of the hostages, the Pakistani army said: “The success underscores the importance of timely intelligence sharing and Pakistan’s continued commitment toward fighting this menace through cooperation between two forces against a common enemy.”