England, who still won the series 4-1, were on course for their first 5-0 home ODI sweep when Pakistan, chasing 303 for victory, slumped to 77 for three after fast bowler Mark Wood took two wickets in three balls.
But Shoaib Malik (77) and Ahmed turned the tide during their stand of 163 — a new fourth-wicket record for either side in an England-Pakistan ODI, surpassing the 122 put on by Pakistan’s Ramiz Raja and Salim Malik at Nagpur in 1989.
Roy faced 89 balls, including eight fours and two sixes. There was no century either for left-handed batsman Stokes, who was out when he flicked Hasan to Umar Gul at short fine-leg.
Both batsmen eventually holed out to debutant spinner Liam Dawson, but the Hampshire left-armer finished with an expensive two for 70 in his eight overs in the Welsh capital.
Imad Wasim, born in the nearby south Wales city of Swansea, hit the winning runs when he struck a four off all-rounder Chris Jordan as Pakistan finished on 304 for six with 10 balls to spare.
Mohammad Rizwan was 34 not out and left-hander Wasim 16 not out against an England attack missing the rested trio of first-choice spinners Moeen Ali and Adil Rashid, as well as fast bowler Liam Plunkett.
Earlier, Jason Roy made 87 and Ben Stokes an ODI best 75 in England’s 302 for nine.
At 244 for five off 40 overs England — who posted a world record 444 for three in the third ODI in Nottingham — were set for another big score.
But Pakistan pegged them back with paceman Hasan Ali taking an ODI best four for 60 and left-arm quick Mohammad Amir three for 50.
Pakistan lost Sharjeel Khan — one of their few hard-hitting batsmen — for 10 when the opener chipped Chris Woakes to mid-on.
Fast bowler Wood then took two wickets in three balls to reduce Pakistan to 77 for three in the 14th over, bowling Babar Azam and having Pakistan captain Azhar Ali (33) caught behind on review.
But Malik twice lofted Dawson for six and Ahmed handed out similar treatment to all-rounder Woakes.
Wicket-keeper Ahmed was first to fifty, off 44 balls, when he pulled Stokes for his seventh four, and Malik followed him to the landmark in 49 balls.
But Ahmed, who made Pakistan’s first ODI hundred at Lord’s in the second match of this series, fell just short of another century when he lofted Dawson and Alex Hales, back-pedalling at mid-on, held the catch.
Pakistan were 266 for six after Malik drove Dawson to Roy at long-off and Mohammad Nawaz was run out by wicket-keeper Jonny Bairstow but those wickets came too late to deny the tourists a much-needed win.
England made a rapid start after Azhar won the toss on a green-tinged pitch and under overcast, swing-friendly, skies.
Hales struck Amir, recalled in place of the injured Mohammad Irfan, for three successive fours in as many balls.
But Amir, switching to around the wicket, had his revenge two balls later when Hales (23) chipped a deliberately slower delivery to Malik at mid-on.
The lively Hasan then dismissed Joe Root for a mere nine when the batsman inside-edged an attempted drive on to his stumps.
England were 92 for three after Wasim struck third ball when England captain Eoin Morgan (10) tamely chipped a full toss straight back to the left-arm spinner.
Wasim, a rare shining light for Pakistan throughout the series, finished with fine figures of one for 33 in his maximum 10 overs.
In sight of what would have been his fourth ODI century, Roy top-edged a pull off Amir’s clever slower-ball bouncer to Hasan at deep square leg.