The Haqqani network is based in the tribal areas of Pakistan, which may have played a role in the prisoner negotiations. Mr. Ghani’s announcement Tuesday came a day after a visit to Kabul by Faiz Hameed, director general of the Inter-Services Intelligence agency, Pakistan’s military intelligence service, and Suhail Mahmood, Pakistan’s deputy foreign secretary.

The two professors were abducted in August 2016 by gunmen while they were traveling in their car. Shortly after they were kidnapped, Navy SEAL team members tried to rescue them from a remote compound in eastern Afghanistan. But the commandos missed the hostages by hours, officials said. In April, the military made another rescue attempt along Afghanistan’s eastern border with Pakistan but narrowly missed them again, officials said.

In a 2017 statement, the Taliban said Mr. King had heart and kidney problems. In a video, one of two that the militants released, Mr. Weeks pleaded with President Trump to save him: “If we stay here for much longer, we will be killed. I don’t want to die here.”

Mr. King’s illness was worsening, and he sometimes lost consciousness, according to the Taliban. “We have tried to treat from him time to time, but we do not have medical facilities as we are in a war situation,” the statement said.

Mr. Trump has made freeing American hostages a priority. The administration has managed to free about 10 hostages held in captivity overseas, using diplomatic leverage or relying on countries such as France and the United Arab Emirates to carry out high-risk military raids in Africa and Yemen. Robert C. O’Brien, Mr. Trump’s national security adviser, continues to ensure these cases receive a high level of attention. Mr. O’Brien was previously the State Department’s special presidential envoy for hostage affairs.

But other high-profile hostages remain intractable problems for this administration, as they were in previous ones. The White House has been unable to win the freedom of Robert Levinson, the former F.B.I. agent and C.I.A. analyst who was abducted in Iran in 2007, or Austin Tice, an American journalist who disappeared in Syria in 2012.

Another American held hostage, Caitlan Coleman, and her family were rescued by the Pakistani military in October 2017. She and her Canadian husband, Joshua Boyle, had been held for five years. The two were abducted by the Haqqani network while backpacking in Afghanistan. Ms. Coleman had three children while in captivity, but a fourth apparently died.