The family were released as part of an operation conducted by Pakistani security forces, according to a Pakistan army statement that was later confirmed by US officials.
The couple, American Caitlan Coleman, 31, and her Canadian husband, Joshua Boyle, 33, were kidnapped in 2012 while they were backpacking Afghanistan. Coleman was pregnant when she was kidnapped and the couple had two more children in captivity.
Pakistan said it had secured their release after receiving intelligence from the US. "The operation by Pakistani forces, based on actionable intelligence from US authorities was successful; all hostages were recovered safe and sound and are being repatriated to the country of their origin," the statement said.
US officials said intelligence about the family's location had been shared with Pakistan officials in recent days.
Boyle's parents, Patrick and Linda, told CNN that they had spoken to their son and that all of the family members were in good health. They learned for the first time in the call that they now have a granddaughter.
Arrangements were being made to return the family either to the US or Canada.
Pakistan official: No deal
Some of the circumstances surrounding the family's release remained unclear late Thursday.
A senior US official told CNN that US intelligence assets had detected and monitored the movement of vehicles believed to be transporting the family. US officials provided this information to Pakistani authorities and US officials even began unilaterally discussing a possible US-staged rescue attempt.
However, to the surprise of the US government, the Pakistani authorities called back their US counterparts to say they had taken custody of all five family members. "That was a surprise to us," the official said.
According to the US official, it was not clear what the Pakistanis "said or did" on the ground to get the family back. But the official added there was no evidence that any Haqqani prisoners held by the Afghan government were released in a prisoner exchange. The US described the Pakistani operation a "transfer of custody," the official said.
A Pakistani military official told CNN his government received the US intelligence about the movement of the hostages on Wednesday at 4:30 p.m. Pakistani time. The Pakistanis launched their operation three hours later.
The official said the recovery operation was conducted by Pakistani intelligence agents with the Pakistani military helping to secure the perimeter. He said that the operation took place in the Federally Administered Tribal Areas in northwestern Pakistan while the family was being transported from one location to another.
The operation involved a shootout with the kidnappers with some being killed in the firefight and others being arrested, according to the official.
He added that the hostages were retrieved in a "rescue operation" and there was "no deal" with the Haqqani Network.
After the hostages were recovered, they were taken to the town of Kohat in Khyber Pakhtunkhwa province in northwestern Pakistan. From there they were taken to the Pakistani capital, Islamabad, where Coleman and Boyle met with their countries' respective officials. The meetings lasted for about six hours and the family were expected to remain in Pakistan for about 24 hours, the official said.
Parents: Son fears arrest
A senior US official said Joshua Boyle refused to board a US military plane that was on standby to take the faily to the US because of fears that he would face arrest.
Boyle was previously married to the sister of Omar Kadhr, a Canadian imprisoned for 10 years at the US detention facility in Guantanamo Bay, Cuba, after fighting US troops in Afghanistan.
Kadhr later sued the Canadian government for violating international law by allegedly not protecting him and conspiring with his US captors, who he says abused him.
The senior US official said there were some questions surrounding Boyle's past. But a Department of Justice spokesman, Wyn Hornbuckle, said US authorities did not intend to arrest Boyle.
"Throughout the captivity of Boyle and Coleman, the department focused its efforts on supporting their recovery and identifying and holding accountable those responsible for taking them hostage," Hornbuckle said. "Coleman and Boyle are not charged with any federal crime and, as such, we do not seek their arrest."
Trump: Pakistan starting to respect US again
President Donald Trump thanked Pakistan for its role in recovering the hostages. "The Pakistani government's cooperation is a sign that it is honoring America's wish that it do more to provide security in the region," Trump said.
"I want to thank Pakistan. They worked very hard on this and I believe they are starting to respect the United States again," Trump added.
In his recently announced strategy for Afghanistan and the wider region, Trump advocated a tougher approach to Pakistan in an effort to persuade Pakistani authorities to crack down harder on the Taliban.
"We hope to see this type of cooperation and teamwork in helping secure the release of remaining hostages and in our future joint counterterrorism operations," Trump said in an earlier statement.
More US hostages
US intelligence officials believed the couple were being held by the Haqqani Network, a branch of the Taliban believed to be responsible for some of the group's most violent and sophisticated attacks. In December the commander of US forces in Afghanistan, Gen. John Nicholson, said the Haqqani Network held a total of five American hostages.
The Taliban released a "proof of life" video of Coleman, Boyle and their two children in December 2016 where Coleman addressed President Barack Obama and then President-elect Trump, saying the Taliban "are not going to simply release our family easily, because it is correct. They want money, power and friends. ... We are told there are Afghans who are prisoners in Kabul that these men care about."
The Afghan government has captured several senior members of the Haqqani Network and US officials believed the Taliban faction had hoped to exchange American hostages for their release.
The US military has long believed that Pakistan's principal intelligence organization, the Inter-Services Intelligence (ISI) agency, has maintained links with the Taliban, particularly the Haqqani Network.
"I think it's clear to me that the ISI has connections with terrorist groups," Gen. Joseph Dunford, the Chairman of the Joint Chiefs of Staff, told a Senate Armed Services Hearing on Afghanistan and South Asia last week, using the Pakistan intelligence agency's acronym.
Trump has previously slammed Pakistan for not doing enough to combat terrorist groups like the Haqqani Network.
Secretary of Defense James Mattis has already withheld millions in military funding from Pakistan due to the Pentagon's view that Islamabad has not "taken sufficient action against the Haqqani Network."
The Taliban continue to hold other western hostages including US citizen Kevin King, 60, and Australian citizen Timothy Weeks, 48. Both men were working as teachers at the American University of Afghanistan in Kabul when they were forcibly kidnapped from a vehicle in August 2016.
In September, three administration officials told CNN that US Special Operations Forces from SEAL Team 6 attempted to rescue the two teachers shortly after they were kidnapped but the captives were not at the location the US forces raided.
One other American is believed to be held hostage in Afghanistan or Pakistan: writer Paul Overby, who is in his 70s.