Captain Eoin Morgan won the toss for England and decided to bat first, but disciplined Pakistan bowling and fielding restricted them to 216 runs.
In reply, Pakistan were tottering at 41 for three in the 10th over, but Hafeez followed up his match-winning 151 in the second innings of the third Test in Sharjah with a fluent unbeaten 102 runs in 130 balls.
It was his 11th ODI century in 170 matches, and helped Pakistan overcome England’s target with 6.2 overs to spare.
The 35-year-old Hafeez added 70 runs for the fourth wicket with Shoaib Malik (26) and 106 runs for the unbroken fifth wicket with Babar Azam, who scored 62 in as many balls.
“I think Babar Azam was just outstanding. When he came in, we were under pressure. But the way he played, he gave confidence to me as well,” said Hafeez, who was named man of the match and later dedicated his century to Younus Khan, who had just announced his retirement from ODIs.
Hafeez added: “The new ball was doing a bit early and their bowlers were bowling very well. I just thought to take it one ball at a time.”
Younus took a good catch at first slip to dismiss Alex Hales early in England’s innings and made nine before a mistimed pull off Reece Topley, who bowled superbly for his three for 26, saw him offer a simple catch to Adil Rashid at mid-on.
England’s decision to bat first backfired when they lost the first three wickets with just 14 runs on the board by the fourth over.
Morgan (76) and the in-form James Taylor (60) added 133 runs for the fourth wicket and propped up the innings. Batting certainly looked easy with those two on the crease, but, once the partnership was broken, when Morgan was caught behind the wicket by Sarfraz Ahmed off the bowling of Shoaib Malik, wickets continued to tumble and England were all out with two balls of their innings to spare.
Pakistan fast bowler Mohammad Irfan was the most successful bowler, taking three for 35 in his 10 overs. Irfan’s opening partner, Anwar Ali, took two for 32, while Malik finished on two for 45.
Morgan blamed his batsmen for the defeat. He said: “We slipped up early on and towards the back end of our batting.
“We have a lot to learn, a lot to pick up, to get us back on terms. It would be cruel to criticise the bowlers with only 220 on the board. We probably could have done with another 30 odd runs. The batters today, for the first time in two series, underperformed.”
In a statement issued before the match, Younus said: “I am proud to announce that, after playing for Pakistan for 15 years, I am announcing my retirement.
“I consulted with my close friends and family members before taking this decision.
“As a batsman I played a positive role in the team’s wins in one-day cricket, but I think this is the right time for me to go.
“I’ll leave some legacy that Younus Khan is still in good shape and, if you want, he can still play one-day cricket.
“But I will be 38 in five or six days’ time, so this is the right time to quit one-day cricket.”
The second match of the four-match series will be played in Abu Dhabi tomorrow.