The 33-year-old right-hander smashed a career best unbeaten 140 to steer Pakistan to an imperious 326-5 before Hafeez the bowler took 2-35 to dismiss Sri Lanka for 213 in 44.4 overs at Sharjah stadium.
The win gives Pakistan a 2-1 lead in the five-match series.
Hafeez accounted for the dangerous Dinesh Chandimal (36) and Seekkugge Prasanna (22) in an incisive spell to thwart any hopes Sri Lanka had harboured.
Paceman Umar Gul, playing his first match since March this year after a knee operation, gave Pakistan a sound start by dismissing Kusal Perera (seven) and Dimuth Karunaratne (nought) in his first spell of five overs and then had the last man Lasith Malinga to finish with 3-19.
Opener Tillakaratne Dilshan hit ten boundaries in an aggressive 62-ball knock of 59 but fell to Shahid Afridi as the Sri Lankan innings fell apart.
Kumar Sangakkara also hit a six in his 14 before he fell to paceman Junaid Khan who took 2-40.
Skipper Angelo Mathews chipped in with 44 but his dism