Dubai: Pakistani bowlers held their nerves to stop threatening Sri Lankan team to win the third one-day international by 21 runs to take a 2-1 lead in the five-match series
The pair combined for a 151-run opening stand that set up Pakistan's innings of 8-257, before the tourists were bowled out for 236 despite a century partnership between captain Tillakaratne Dilshan (64) and Kumar Sangakkara (45).
The result sees Pakistan take a 2-1 lead in the five-match series.
Hafeez's 83 of 101 balls ended when the right-hander was bowled by Seekkuge Prasanna, before Farhat (70 off 82) hauled a Dilshan delivery out to Mahela Jayawardene at long on.
Despite the foundations being set for a substantial total, Pakistan failed to capitalise on the enterprise of their openers and suffered a spate of fallen wickets after Farhat's dismissal.
Abdur Razzak (6), Umar Akmal (7) and Misbah-ul-Haq (7) fell in quick succession to leave their side at 193-5, before Younis Khan (42 off 52) steered them past the 250 mark.
Leg-spinner Prasanna (2-39) and paceman Lasith Malinga (2-37) kept the Pakistan total in check.
Sri Lanka lost Upul Tharanga early in their chase but gained the ascendancy when Dilshan and Sangakkara added 106 for the second wicket.
The match turned through the heroics of Shahid Afridi, whose direct hit had Sangakkara run out for 45 before the Pakistan spinner bowled Dilshan for 64.
Dinesh Chandimal (31) and Jaywardene (17) restored stability to the chase only for the intervention of Saeed Ajmal (3-42), who removed both in quick succession.
Angelo Mathews produced an energetic 32 off 40 to keep the challenge alive, but he could not find a partner as Malinga, Jeevan Mendis and Thisara Perera fell around him.
Gul (3-48) then dismissed Prasanna and Dilhara Fernando to seal the win.
The fourth ODI will be played in Sharjah on Sunday.
Brief scores
Pakistan 257 for 8 (Hafeez 83, Farhat 70, Prasanna 2-39)
Sri Lanka 236 (Dilshan 64, Ajmal 3-42, Gul 3-48)
Result Pakistan beat Sri Lanka by 21 runs
MOM Mohammad Hafeez (Pakistan)