Media playback is unsupported on your device Media caption The US president, who was joined at the White House by Sgt Bergdahl's parents, Robert and Jani, said ''he was never forgotten''

A US soldier who has been held by the Taliban in Afghanistan for nearly five years has been freed in deal that includes the release of five Afghan detainees, US officials say.

US Army Sergeant Bowe Bergdahl, 28, was handed to US forces in good health.

The five Afghan detainees were released from the US prison at Guantanamo Bay, Cuba, and were handed over to Qatar, which mediated the deal.

President Barack Obama said the US "shared the joy" of the release.

Sgt Bergdahl was the only US soldier being held by the Taliban in Afghanistan.

Officials said he was in good condition and undergoing medical tests at Bagram Air Field, the main US base in Afghanistan.

He would later be flown to a US military medical centre in Germany to "decompress" after his ordeal, American defence sources told the AFP news agency.

Who are the Guantanamo detainees?

Image copyright AFP Image caption The five released inmates had all been held at Guantanamo since 2002

Mohammad Fazl served as the Taliban's deputy defence minister during America's military campaign in 2001. Accused of possible war crimes, including the murder of thousands of Shia Muslims.

Khirullah Khairkhwa was a senior Taliban official serving as interior minister and governor of Herat, Afghanistan's third largest city. Alleged to have had direct links to Osama bin Laden.

Abdul Haq Wasiq was the Taliban's deputy minister of intelligence. Said to have been central in forming alliances with other Islamic fundamentalist groups to fight against US and coalition forces.

Mullah Norullah Noori was a senior Taliban military commander and a governor. Also accused of being involved in the mass killings of Shia Muslims.

Mohammad Nabi Omari held multiple Taliban leadership roles, including chief of security. Alleged to have been involved in attacks against US and coalition forces.

'Never forgotten'

Sgt Bergdahl's parents, Robert and Jani, joined President Obama at the White House on Saturday, hours after the president told them of their son's release.

Offering thanks to those who took part in his son's recovery, Robert Bergdahl said his son was having trouble speaking English after his rescue.

Media playback is unsupported on your device Media caption US Army Sgt Bowe Bergdahl was held by the Taliban in Afghanistan for nearly five years

President Obama told reporters that while Sgt Bergdahl was gone "he was never forgotten", adding that the US had an "ironclad commitment" to bringing home its prisoners of war.

He confirmed the Qatari government had given the US assurances "that it will put in place measures to protect our national security".

Officials said the Taliban had handed him over on Saturday evening, local time, in eastern Afghanistan.

Several dozen US special forces were involved in the exchange, they said, which took place near the Pakistani border.

Image copyright AP Image caption The soldier's hometown of Hailey continued to highlight his captivity

A senior official told the BBC that, once aboard the US helicopter, Sgt Bergdahl wrote "SF?" - asking if they were special operations forces - on a paper plate and showed it to the pilots, who replied: "Yes, we've been looking for you for a long time."

The senior official said: "At that point, Sgt Bergdahl broke down".

The soldier, of Hailey, Idaho, was captured on 30 June 2009, about two months after arriving in eastern Afghanistan.

US Defence Secretary Chuck Hagel said Sgt Bergdahl would be given "all the support he needs to help him recover from this ordeal, and we are grateful that he will soon be reunited with his family".

Image copyright AP Image caption Sgt Bergdahl's father, seen here at a rally last June, has been tirelessly campaigning for his son's release

Mr Obama thanked the emir of Qatar for his role in enabling the transfer to take place.

The prisoners were thought to be the most senior Afghans still held at Guantanamo.

The Taliban said they welcomed their release with "great happiness", AFP reports.

Under the deal, the detainees will be banned from leaving Qatar for at least a year.

In January, the US military obtained a new video of Sgt Bergdahl, giving his family renewed hope of his eventual return.

Throughout his captivity, the soldier's hometown had continued to remember him with special events and yellow ribbons tied to utility poles and trees.