ISLAMABAD — The Taliban freed an American and an Australian held hostage since 2016 on Tuesday, in exchange for three top Taliban figures who were released by the Kabul government and flown out of Afghanistan the previous day.

The hostages — American Kevin King and Australian Timothy Weeks — were released in southern Zabul province, ending more than three years of captivity since they were abducted outside the American University in Kabul, where both work as teachers.

Kings’ family issued a statement later Tuesday saying he was now safe with U.S. officials in Afghanistan and getting the medical care he needs ahead of his return home to be reunited with his family. It was not clear if Weeks was also with Australian officials.

The two were released in Zabul’s Naw Bahar district, a region largely under Taliban control, according to a Taliban official who spoke on condition of anonymity because he wasn’t authorized to talk to the media. King and Weeks were handed over to U.S. forces and transported from the area in a U.S. helicopter.

“We are so happy to hear that my brother has been freed and is on his way home to us,” said King’s sister, Stephanie Miller. “This has been a long and painful ordeal for our entire family, and his safe return has been our highest priority. We appreciate the support we have received and ask for privacy as we await Kevin’s safe return.”

Their freedom came hours after the Afghan government freed three Taliban prisoners and sent them to Qatar. They included Anas Haqqani, the younger brother of the Taliban’s deputy Sirajuddin Haqqani, who also leads the fearsome Haqqani network. The other two were Hajji Malik Khan, an uncle of Haqqani and a Haqqani lieutenant, Hafiz Abdul Rashid.

It appears the Taliban had refused to hand over the two professors until they received proof their men had reached Qatar. A Taliban statement said the prisoner exchange was a good step “for building trust” and something that “can help in peace process.”

The American University of Afghanistan confirmed the release of the two, saying that its “community shares the relief of the families of Kevin and Timothy, and we look forward to providing all the support we can to Kevin and Tim and their families.”

Pakistan’s Prime Minister Imran Khan also welcomed the professors’ freedom, saying he appreciates “steps taken by all involved to make it possible.” Pakistan wields some influence over the Taliban and has played a behind-the-scenes role in trying to restart peace talks.

Afghan President Ashraf Ghani a week ago announced the “conditional release” of the three ranking Taliban figures, saying at a press event broadcast live on state television that it was a very hard decision he felt he had to make in the interest of the Afghan people.

Kings family gave a special mention to Washington’s peace envoy Zalmay Khalilzad, U.S. Secretary of State Mike Pompeo and National Security Adviser Robert O’Brien for their “behind the scenes” efforts to get Ghani on board with the prisoner swap. Pompeo and O’Brien made separate calls to Ghani on Monday.

In 2017, several months after their abduction, the Taliban released two videos showing the captives. A January 2017 video showed them appearing pale and gaunt. In the later video, King and Weeks looked healthier and said a deadline for their release was set for June 16 that year.

Both said they are being treated well by the Taliban but that they remain prisoners and appealed to their governments to help set them free. It was impossible to know whether they were forced to speak.

Subsequently, U.S. officials said that American forces had launched a rescue mission to free the two, but the captives were not found at the raided location.

The prisoner exchange was intended to try to restart talks to end Afghanistan’s 18-year war and allow for the eventual withdrawal of U.S. troops from Afghanistan.

The United States had been close to an agreement in September with the Taliban but a fresh wave of violence in the Afghan capital that killed a U.S. soldier brought talks and an impending deal to a grinding halt.

The agreement called for direct talks between the Taliban and Afghan government as well as other prominent Afghans to find a negotiated end to the war and set out a roadmap for what a post -war Afghanistan would look like.

Ghani in his discussions with Pompeo and O’Brien said he wanted a reduction in violence and an all-out cease-fire, his spokesman said.

According to a U.S. State Department statement Tuesday, Pompeo told Ghani the United States was “committed to work closely together to address violence if the President’s decision does not produce the intended results.”

Southern Zabul province, where the two professors were freed, is heavily controlled by the Taliban and vast parts of it have long been a no-go area for the government.

But according to the Taliban, an unofficial cease-fire is now being observed in three districts of the province — Shahjoy, Shahmatzo and Naw Bahar — possibly to facilitate the release of the two hostages.

Associated Press writers Abdul Khaliq in Lashkar Gah, Afghanistan, and Tameem Akhgar in Kabul, Afghanistan, and Matthew Lee in Washington contributed to this report.