Hafeez finished with 113 not out for his ninth one-day hundred as Pakistan knocked off their modest 226-run target in the 42nd over to take an unbeatable 3-1 lead ahead of Friday's fifth and final match.
Pakistan had restricted Sri Lanka to 225 all out in 48.5 overs with Ajmal (4-39) and paceman Umar Gul (3-37) sharing the spoils.
Hafeez, who hit hundreds in Pakistan's two wins in the first and third matches in Sharjah, pushed Tillakaratne Dilshan for his 12th boundary to complete three figures, raising his bat and blowing a kiss to his wife in the stands.
In all Hafeez hit two sixes and 12 boundaries off 119 balls, while Sohaib Maqsood finished with a 56-ball 46 not out with a six and four boundaries.
Also Read
Hafeez and Ahmed Shehzad (44) put Pakistan on track during a solid 84-run stand after opener Sharjeel Khan was dismissed for 13.
Shehzad, who hit a career best 124 in Pakistan's two-wicket loss in the second match in Dubai, started off well with three boundaries in Nuwan Kulasekara's third over while Hafeez matched his partner with as many boundaries in Suranga Lakmal's seventh.
Shehzad hit five boundaries during his 56-ball knock as Sri Lanka's bowlers were rendered ineffective.
Maqsood and Hafeez shared an unbroken 111-run stand for the third wicket.
This becomes Pakistan's seventh one-day series win in the year -- their best-ever in a calendar year, beating the six series win record achieved in 2011.
"It was a really pleasing performance by the team," said Pakistan captain Misbah-ul-Haq.