Bowe Bergdahl before his capture by the Taliban in Afghanistan. Photo: Getty Images

Bowe Bergdahl’s release capped months of secret negotiations in which the government of Qatar passed messages between the United States and the Taliban.

The Obama administration first pressed for Bergdahl’s freedom in 2011, offering to transfer five high-level Taliban prisoners to Afghan custody in a long-shot bid for peace with the insurgents.

Both the Afghan government and the Taliban had sought the release of the militants from Guantanamo Bay as far back as 2005.

President Obama with Bob and Jani Bergdahl on Saturday.Photo: AP

But the talks collapsed in March 2012 when the Taliban walked away from the table, accusing the US of not bargaining in good faith.

In November, the Taliban unexpectedly sent word they were willing to again discuss Bergdahl’s release — but only through intermediaries, The Washington Post reported.

Working with the Qatari government, the US demanded proof that Bergdahl was still alive, and in January received a never-released video that reportedly showed him looking “very gaunt” and “haggard.”

The US then sweetened its offer regarding the five detainees by agreeing to spring them all at once, after previously insisting they leave in stages to try to keep them from taking up arms again, the paper reported.

In a March report to Congress, Director of National Intelligence James Clapper said 29 percent of former Gitmo detainees were confirmed to have resumed terrorist or insurgent activities, or suspected of having done so.

National Security Adviser Susan Rice told CNN’s “State of the Union” on Sunday that Qatar provided “a series of very specific assurances” that the detainees would be closely monitored.

Rice said President Obama participated in the negotiations.

“President Obama spoke to the emir of Qatar on Tuesday, when this looked like it was a real possibility,” Rice said.