U.S. army Sgt. Bowe Bergdahl, a Taliban captive for five years after abandoning his post in Afghanistan, pleaded guilty Monday to desertion and misbehaviour-before-the-enemy charges that could put him in prison for life.

"I understand that leaving was against the law," said Bergdahl, whose decision to walk off his remote post in Afghanistan in 2009 prompted intense search and recovery missions, during which some of his comrades were seriously wounded.

"At the time, I had no intention of causing search and recovery operations," Bergdahl said at the hearing in Fort Bragg, N.C., but he added that now he does understand that his decision prompted efforts to find him.

Bergdahl, 31, is accused of endangering his comrades by abandoning his post without authorization. He told a general after his release from five years in enemy hands that he did it with the intention of reaching other commanders and drawing attention to what he saw as problems with his unit.

The prosecution made no agreement to limit Bergdahl's punishment in return for the soldier's guilty pleas. The judge, Army Col. Judge Jeffery R. Nance, reminding Bergdahl that he could spend the rest of his life in prison, and asked him one last time if he wanted to plead guilty. "Yes," Bergdahl replied, and the judge accepted them.

Bergdahl may be hoping for leniency. Misbehaviour carries a maximum penalty of life in prison, while desertion is punishable by up to five years.

Bergdahl 2016

Bergdahl is seen during a pretrial military hearing on July 7, 2016 in Ft. Bragg, N.C. The misbehaviour charge he has pleaded guilty to carries the possibility of a sentence of life in prison. (Sara D. Davis/Getty Images)

His decision to plead guilty rather than face trial marks another twist in an eight-year drama that caused the nation to wrestle with difficult questions of loyalty, negotiating with hostage takers and America's commitment not to leave its troops behind. U.S. President Donald Trump has called Bergdahl a "no-good traitor" who "should have been executed."

Prisoner swap 

The decision by Bergdahl leaves open whether he will return to captivity for years — this time in a U.S. prison — or receive a lesser sentence that reflects the time the Taliban held him under brutal conditions. He says he had been caged, kept in darkness, beaten and chained to a bed.

Freed three years ago, Bergdahl had been scheduled for trial later this month, opting to for a judge rather than a military jury.

Bergdahl was a 23-year-old Pte. 1st Class in June 2009 when, after five months in Afghanistan, he disappeared from his remote infantry post near the Pakistan border, triggering a massive search operation.

Videos soon emerged showing Bergdahl in captivity by the Taliban, who ruled Afghanistan in the years before the Sept. 11, 2001, attacks and harboured al-Qaeda leaders including Osama bin Laden. For years, the U.S. kept tabs on Bergdahl with drones, spies and satellites as behind-the-scenes negotiations played out in fits and starts.

In May 2014, he was handed over to U.S. special forces in a swap for five Taliban detainees at the Guantanamo Bay prison, fuelling an emotional U.S. debate about whether Bergdahl was a hero or a deserter.

As critics questioned whether the trade was worth it, then President Barack Obama stood with Bergdahl's parents in the White House Rose Garden and defended the swap. The United States does not "leave our men or women in uniform behind," Obama declared, regardless of how Bergdahl came to be captured. The Taliban detainees were sent to Qatar.

"Whatever those circumstances may turn out to be, we still get an American soldier back if he's held in captivity," Obama said. "Period. Full stop."

Trump, as a presidential candidate, was unforgiving of Bergdahl, who has been assigned to desk duty at a Texas army base pending the outcome of his case. At campaign events, Trump declared that Bergdahl "would have been shot" in another era, even pantomiming the pulling of the trigger.

"We're tired of Sgt. Bergdahl, who's a traitor, a no-good traitor, who should have been executed," Trump said at a Las Vegas rally in 2015.

Soldiers sent on search missions angry

Bergdahl's guilty plea follows several pretrial rulings against him that had complicated his defence. Army Col. Jeffery R. Nance, the judge, decided in June that testimony from troops wounded as they searched for him would be allowed during sentencing, a decision that strengthened prosecutors' leverage to pursue stiffer punishment.

Some of Bergdahl's fellow soldiers want him held responsible for any harm suffered by those who went looking for him. The judge ruled a Navy SEAL and an army National Guard sergeant wouldn't have found themselves in separate firefights if they hadn't been searching.

The defence separately argued that Trump's scathing criticism unfairly swayed the case. The judge ruled otherwise. Nance wrote in February that Trump's comments were "disturbing and disappointing" but didn't constitute unlawful command influence by the soon-to-be commander in chief.

Bergdahl's lawyers also contended that misbehaviour before the enemy, the more serious charge, was legally inappropriate and too severe. They were rebuffed again. The judge said a soldier who leaves his post alone and without authorization should know he could face punishment. The misbehaviour charge has rarely been used in recent decades, though there were hundreds of cases during World War II.

Defence lawyers didn't dispute that Bergdahl walked off his base without authorization. Bergdahl himself told a general during a preliminary investigation that he left intending to cause alarm and draw attention to what he saw as problems with his unit. An Army Sanity Board Evaluation concluded he suffered from schizotypal personality disorder.

The defence team argued that Bergdahl couldn't be held responsible for a long chain of events that included decisions by others about how to retrieve him that were far beyond his control.

The Hailey, Idaho, native had been assigned to desk duty at a Texas army base while his case unfolded.