WASHINGTON — An American woman and her family who have been held hostage by militants in Afghanistan for five years have been freed, ending a case that has long frustrated diplomats and F.B.I. agents involved in trying to secure their release, according to people familiar with the situation.

The family was in Pakistan early Thursday, and the White House was preparing to release a statement once they are safely in American hands, according to people who described their status on condition of anonymity because the details have not yet been made public.

Media in the region reported that the Pakistani military had initially taken custody of the family.

The Run-Up

The podcast that makes sense of the most delirious stretch of the 2016 campaign.

The family was freed with the help of another country, the person said, but it was not clear what if any concessions were made to the Haqqani network, the Taliban faction that had seized Caitlan Coleman, 31, and her Canadian husband, Josh Boyle, 34.

The pair was taken in October 2012 while backpacking in the Wardak Province, a militant stronghold near Kabul. At the time of her abduction, Ms. Coleman was pregnant. She had two more children in captivity, adding pressure to resolve an already desperate situation.

Previously the Haqqani network had demanded the release of Anas Haqqani, one of their commanders. The Afghan government managed to capture him in 2014, and he was sentenced to death. The group had threatened to kill the family if the Afghans executed him.

In December 2016, the militants released a video of the family, including footage depicting her two children. She described her time as a hostage as “Kafkaesque” and said she had been “defiled.”

“Indeed they threaten to retaliate against our family,” she said. “Their group will do us harm and punish us. So we ask that you are merciful to their people and, God willing, they will release us.”

An earlier video made public in August 2016 showed the couple. During it, Ms. Coleman urged the American government to “help stop this depravity.” She also said her captors “will execute us.”

Earlier attempts to bring the family home had been unsuccessful. 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 it didn’t happen for reasons that remain unclear. The previous 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.

The Haqqanis are believed to be holding another American university professor who was kidnapped in August 2016. Earlier this year, the group released of video of the man, Kevin King, who pleaded for President Trump to free him. “Have mercy on me and get me out.”

Another American, Paul Overby, disappeared in May 2014 and has not been seen since his abduction. He was trying to interview the head of the Haqqani network when he was abducted.