The tourists achieved the sixth highest successful chase in history when they surpassed the target of 377 runs in the post-lunch session of the fifth day's play in Pallekele, finishing on 382-3.
Skipper Misbah-ul Haq made 59 not out, smashing the winning six off Jehan Mubarak to end his nation's long winless streak in Sri Lanka where they lost the last three series in 2009, 2012 and 2014.
It was the first time a visiting team had scored more than 300 runs in the fourth innings to win a Test on Sri Lankan soil.
Younis, who made his 100th Test appearance in the second match, ended the game just 19 runs away from surpassing Javed Miandad's tally of 8,832 runs to become Pakistan's leading scorer in Tests.
Pakistan added 25 runs to their overnight score when they lost the wicket of Shan Masood, who was stumped by Dinesh Chandimal off Tharindu Kaushal for 125.
The Kuwait-born left-hander's maiden Test century came during a third-wicket stand of 242 with Younis after Pakistan had been reduced to 13-2 on Monday afternoon.
The 37-year-old Younis, who came to bat after the fall of the second wicket, stayed solid at one end during a seven-hour vigil in which he hit 18 boundaries.
Younis' five hundreds in the fourth innings -- in a career tally of 30 -- are the most by any batsman.
Misbah provided able support during a 127-run stand with Younis, mixing caution with aggression which fetched him eight fours and a six.
Pakistan had won the first Test in Galle by 10 wickets and Sri Lanka took the second in Colombo by seven wickets.
The two teams will now play a limited-overs series of five one-day internationals, followed by two Twenty20 games, from Saturday.
