The three Taliban prisoners were flown to Doha, Qatar, where the Taliban have a political office, as part of a highly choreographed exchange, officials said. After the confirmation that the Western hostages had been freed, Taliban officials said that they had custody of the three men in Doha.

Also to be included in the exchange were 10 members of the Afghan security forces being held by the Taliban, Afghan and American officials said. Later on Tuesday, a Taliban spokesman, Zabihullah Mujahid, said that they had been released. That was not immediately confirmed by Afghan officials.

“All these steps are a positive development for trust building, which can help the peace process,” the spokesman said.

The White House and State Department also alluded to the peace talks in statements that welcomed the Western prisoners’ release. “We see these developments as hopeful signs,” Secretary of State Mike Pompeo said, that the Afghan war “may soon conclude through a political settlement.”

In addition to Mr. Haqqani, the Afghan government released Hafiz Abdul Rashid, a senior Taliban commander who had equipped suicide bombers, chosen their targets and moved them from safe houses in Pakistan into Afghanistan. Mr. Rashid, a brother of a member of the Taliban negotiating team in Doha, was captured along with Mr. Haqqani in 2014.

The third Taliban member in the exchange was Hajji Mali Khan, a senior commander and an uncle of the deputy leader of the Taliban.

The three men are members of the Haqqani network, a brutal wing of the Taliban based in Pakistan’s tribal areas along the border with Afghanistan. The group has been responsible for suicide bombings against Afghan civilians, as well as attacks on Afghan and American forces.