Muhammad Hashim Ibrat, who was posted at the Afghan Consulate in Quetta at the time of his abduction, has "come back safely", Afghan Consul General Ghullam Muhammad Bahadur told reporters.
Armed militants had kidnapped Ibrat on July 28 from Satellite Town area of Quetta while he was travelling from his home to office. He remained in captivity for over a month.
The website of Dawn newspaper quoted an official of the Afghan Consulate as saying that "some important Taliban leaders" were freed in exchange for Ibrat.
The Afghan government was in close contact with Pakistan for the recovery of the diplomat. The Afghan Ambassador in Islamabad was in regular contact with Pakistani officials to ensure his recovery.
No group claimed responsibility for the abduction.
According to figures tabled in the assembly of Balochistan province, around 78 gangs involved in kidnapping and other subversive activities are active in Quetta, which has been hit hard by militant, sectarian and separatist violence.