Ali struck six 4s in his 103 off just 137 balls and was ably supported by first wicketkeeper Sarfraz Ahmed (48) and then Misbah ul Haq (68) who stayed till the end to clinch a historic win for Pakistan.
It is the first time since 1994 that Pakistan has chased a target of over 300 runs.
Azhar Ali made 103 off 137 balls in a 238-minute stay. This was his fifth hundred in Test cricket. He hit only six fours in his long innings.
He was involved in two big successive partnerships – first with Sarfraz for a 89-run fourth wicket stand and then with Misbah for a 109-run fifth wicket partnership.
He was out caught behind off pace bowler Suranga Lakmal in the 57th over when Pakistan were just seven runs away from their memorable victory.
Misbah remained not out on 68, an aggressive innings from 72 balls with four boundaries with Asad Shafiq on one.
Earlier, Sri Lanka were all out for 214 just before lunch as captain Angelo Mathews and Prasanna Jayawardene added 62 for the sixth wicket partnership.
Mathews made 31 and Jayawardene top-scored with 49 before Mahela Jayawardene and Kaushal Silva scored 46 and 36, respectively.
Left-arm spinner Abdul Rehman captured four wickets for 56 while off-spinner Saeed Ajmal and pace bowler Mohammad Talha bagged three wickets each for 53 and 65, respectively.
Pakistan began their chase an uphill task, just after lunch with openers Openers Khurram Manzoor and Ahmed Shehzad made 21 each and veteran batsman Younis Khan scored 29 before wicketkeeper-batsman Sarfraz Ahmed joined Azhar Ali at 97-3.
After the match ended, Azhar Ali was adjudged the Man-of-the-Match for his terrific innings and the winning trophy was shared by both teams for playing a drawn Test series.
Pakistan’s chances seemed to have faded away at 107-3 at tea, as they needed 195 in the last 35 overs. But Ali upped the tempo during a rapid 89-run partnership in 14.5 overs with Sarfraz Ahmed (48) which left Sri Lanka on the defensive.
Even when Sarfraz was out, caught behind off paceman Shaminda Eranga, Pakistan needed 116. Sarfraz hit four boundaries and a six off 46 balls.
But Misbah supported Ali as Pakistan achieved their second best chase in all Test cricket, behind the 314 they chased against Australia in Karachi in 1994.
Sri Lanka had won the second Test in Dubai by nine wickets while the first Test ended in a draw in Abu Dhabi.
Pakistan had lost Ahmed Shehzad (21), Khurram Manzoor (21) and Younis Khan (29) as they were derailed in their chase.
Needing to score at a rate of more than five an over to win, Pakistan were given a rapid 35-run start by the sixth over but paceman Suranga Lakmal (3-79) halted that progress by dismissing openers Shehzad and Manzoor within 13 runs.
Younis Khan tried to increase the pace before being caught off Angelo Mathews two overs before tea, but the scenario changed in the last session.
With the series tied, Pakistan also remained unbeaten in five series in the United Arab Emirates, which has become their home since security fears — sparked in the wake of attacks on the Sri Lankan team bus in Lahore in 2009 — forced them to play at neutral venues.
They twice drew series with South Africa (2010 and 2013) and beat Sri Lanka (2011) and England (2012).
Misbah said Pakistan desperately wanted the win.
“That was really a much wanted win. We wanted the win it for our coach Dav Whatmore and wanted to give him a winning farewell,” said Misbah of the team coach who ends his two-year tenure with this match.
Sri Lankan captain Angelo Mathews regretted batting failure.
“We dominated for three-and-a-half days but unfortunately could not score big in the second innings and lost,” said Mathews, declared man-of-the-series for his 412 runs in three Tests.
Earlier, Sri Lanka were bowled out for 214 in their second innings, with left-arm spinner Abdul Rehman finishing with 4-56 and off-spinner Saeed Ajmal taking 3-53, but not before their batsmen had kept Pakistan at bay on the morning of the fifth and final day.
On an exciting day when both teams had a chance of victory, Mathews (31) and Prasanna Jayawardene (49) shared a 62-run stand for the sixth wicket and batted positively to add 56 in the first hour after the tourists resumed at 133-5.
But Mohammad Talha gave Pakistan their much-needed breakthrough when he had Mathews caught off a miscued hook.
Rehman then grabbed two quick wickets to put the brakes on Sri Lanka, having Dilruwan Perera caught at short-leg for eight before removing Rangana Herath for a first-ball duck.
Herath recorded a golden pair in the match as he was also dismissed first ball in the first innings.
Prasanna hit six boundaries in his 88-ball knock before he was dismissed by Ajmal, who also removed Shaminda Eranga to wrap up the innings.