DOHA, Qatar — When the United States invaded Afghanistan in 2001 and toppled the Taliban government, even those who surrendered were treated as terrorists: handcuffed, hooded and shipped to the American detention camp in Guantánamo Bay, Cuba.

Now, in a stark demonstration of the twists and contradictions of the long American involvement in Afghanistan, five of those men are sitting across a negotiating table from their former captors, part of the Taliban team discussing the terms of an American troop withdrawal.

[To follow the Afghan war peace negotiations, sign up for the weekly At War newsletter.]

“During our time in Guantánamo, the feeling was with us that we had been brought there unjustly and that we would be freed,” said one of the former detainees, Mullah Khairullah Khairkhwa. “But it never occurred to me that one day there would be negotiations with them, and I would be sitting there with them on one side and us on the other.”

The five senior Taliban officials were held at Guantánamo for 13 years before catching a lucky break in 2014. They were exchanged for Sgt. Bowe Bergdahl, the only known American service member to be held by the insurgents as a prisoner of war.