The officials, posted at Consulate General of Pakistan in Jalalabad, were abducted on June 16 while travelling from Jalalabad province to Torkham border crossing, Foreign Office (FO) said.
"(Afghanistan) President Ashraf Ghani personally phoned Pakistan's Charge d'Affiares in Kabul to inform that the Afghan security forces had recovered the two Pakistani officials in a security operation," it said.
The officials were handed over to the Embassy of Pakistan in Kabul by the Afghan Ministry of Foreign Affairs.
They would be flown back to Pakistan to join their families as soon as possible, the FO said.
Jalalabad is the regional hub in eastern Afghanistan, close to the border with Pakistan.
The Taliban and militants linked to the ISIS operate in eastern Afghanistan.