The son of a former Pakistani prime minister who was freed from kidnappers in a dramatic military rescue in Afghanistan has been handed over to Pakistan's ambassador in Kabul.

Ali Haidar Gilani arrived by helicopter at the Afghan ministry of defence in Kabul, where he was greeted with hugs and handshakes by Pakistani ambassador Abrar Hussain.

Mr Gilani, the son of Yusuf Raza Gilani, is expected to be flown home to Pakistan in a special plane sent to Kabul by the Islamabad government.

He was rescued during a joint US-Afghan commando raid on an al-Qaida compound near the Pakistani border on Tuesday, after three years of captivity by militants in Pakistan and Afghanistan.

Mr Gilani spent the night at a military base, undergoing medical check-ups. He arrived at the ministry with long hair and a heavy beard, wearing a T-shirt, combat trousers and a baseball cap.

He thanked Afghan forces for "their sacrifice for someone from another country". He also thanked US forces for providing shelter, food and medical care.

"Right now, I just want to be reunited with my family and get back to normal life," he said.

His father, Yusuf Raza Gilani served as prime minister from 2008 to 2012.

His rescue had been a "nice surprise," the spokesman for US forces in Afghanistan, Brigadier General Charles Cleveland said earlier, as it was not known that Mr Gilani was being held at the location where the operation was taking place.

The raid in the Gayan district of Paktika province was conducted under the authority of the US anti-terrorist mission in Afghanistan, Operation Freedom's Sentinel.

US secretary of defence Ash Carter praised the rescue, calling it "an excellent example of the strong security and intelligence partnership between Afghan and US forces".