A German engineer and four Afghans held hostage in southern Afghanistan for nearly three months have been freed, German and Afghan officials say.
Rudolf Blechschmidt appealed for help in a video broadcast
Rudolf Blechschmidt, 62, was in the custody of the Afghan authorities, German Foreign Minister Frank-Walter Steinmeier said in a statement.
He and the four Afghans were handed over to officials in Wardak province.
They were seized by the Taleban on 18 July along with another German man, who later died during the ordeal.
Kidnappings have soared in Afghanistan in recent months, where foreign and Afghan troops are battling the Taleban.
Mr Steinmeier said the German ambassador in the Afghan capital, Kabul, had spoken to Mr Blechschmidt by telephone, Reuters news agency reports.
"We are all pleased and relieved," Mr Steinmeier said in his statement in Berlin.
The chief of Jaghato district in Wardak province, Mohammad Naeem, said five Taleban prisoners had been released in the swap.
He said they were not senior Taleban commanders but they included the father of the kidnapper. They had been arrested as insurance to ensure the safe release of the hostages, he said.
A Taleban spokesman also told news agencies in Kabul the deal had been done and the five hostages had been freed.
Shortly after they abducted the Germans, the Taleban said they had killed both men because Germany had ignored a demand to withdraw its 3,000 troops from Afghanistan.
But in August Mr Blechschmidt appealed for help in a video broadcast on a private Afghan television channel.
He was seen lying on a sheet on the ground, clutching his chest and coughing on the video.
The other German took ill in captivity and he was shot dead soon after being held. His body was found on 21 July.
Since then negotiations have been going on, brokered by local elders, to secure the release of Mr Blechschmidt and his Afghan colleagues.
The BBC's Alastair Leithead in Kabul says the Taleban have increasingly been using kidnap of foreigners as a strategy in their insurgency, securing the release of prisoners and ransom payments in exchange for their hostages.