Bowe Bergdahl
Bowe BergdahlReuters

Bowe Bergdahl, the U.S. soldier who was held captive in Afghanistan for five years before being freed in a prisoner swap with the Taliban in 2014, pleaded guilty on Monday to desertion and endangering fellow troops for walking away from his unit in 2009.

"I left my observation post on my own," Bergdahl was quoted by AFP as having told a court martial in Fort Bragg, North Carolina.

"I understand leaving was against the law," he added.

Bergdahl's decision to plead guilty put an end to more than two years of legal jousting over a case that inflamed U.S. political divisions over the Afghan war.

Earlier this year Bergdahl's lawyers sought to have the case against him thrown out after President Donald Trump labelled him a "dirty rotten traitor," ostensibly prejudicing the case.

Trump had also called during his run for president for Bergdahl to be put to death for desertion.

The court, however, ruled that Trump's words, even if he was the incoming commander in chief, did not have any weight in the courtroom.

Asked about Bergdahl's guilty plea on Monday, Trump said, "I'm not going to comment on him, but I think people have heard my comments in the past."

Bergdahl, 31, disappeared from Combat Outpost Mest-Malak in rugged Paktika Province, eastern Afghanistan, on June 20, 2009.

After departing the base, leaving behind his firearm, he was quickly captured by militants and held prisoner by the feared Haqqani faction, a Taliban-linked, Pakistan-based organization blamed for many deadly attacks on U.S. soldiers.

He was eventually freed in a May 2014 deal after the United States released five Afghan detainees from Guantanamo Bay.

Critics said that, in making the quiet trade for Bergdahl, the administration violated the 2014 National Defense Authorization Act, which requires a 30-day warning before releasing terrorists from the compound - charges that then U.S. Secretary of Defense Chuck Hagel denied.

A private first class at the time of his disappearance, he was automatically promoted twice in captivity, first to the rank of specialist and then sergeant.

Bergdahl has said he never intended to join the enemy but instead meant to go to another base to report problems in his military unit.

He pleaded guilty to one count of desertion and a second of misbehavior before the enemy, also referred to as endangering troops. The first charge carries a maximum sentence of five years in prison while the second could mean up to life in prison.