Sri Lanka looked to be on the ropes as a late clatter of wickets left them needing 18 off the final two overs in chase of 233 with just two wickets remaining.
But sixes from Chandimal and Ajantha Mendis in the penultimate over put them on the front foot and Mendis sealed victory with a four with two balls to spare.
Chandimal finished with 64 and earlier Lasith Malinga had taken four wickets to help Sri Lanka to victory in the final match of a series they have lost 3-2.
Angelo Mathews' men looked like they were going to cruise to victory in their reply as openers Kusal Perera and Tillakaratne Dilshan engineered them safely through the opening 12 overs.
The former was swinging the bat and sat three runs short of half century off just 41 balls when he fell lbw while attempting to reverse sweep Mohammad Hafeez to fine leg.
That took the score to 75 for one when Kumar Sangakkara came to the crease and he and Dilshan continued to score steadily before the opener fell for 45, hoisting a Junaid Khan delivery into the hands of Saeed Ajmal.
Just four overs later, Sangakkara was on his way back to the pavilion as he was clean bowled by Khan for 22 and all of a sudden Sri Lanka looked like they could be wobbling at 129 for three.
Chandimal was at the other end and it looked like he may have to hold the reply together as Ashan Priyanjan departed for a single run, edging Umar Gul to wicketkeeper Umar Akmal.
Mathews then helped to steady the ship but the run rate dried up as Sri Lanka began to fall behind in the game and when he edged Anwar Ali behind for just eight, they needed more than a run a ball.
A dramatic finish looked on the cards but Chandimal was running out of partners and when Kithuruwan Vithanage was bowled by Ajmal for 14 it was advantage Pakistan.
Sachithra Senanayake holed out to Hafeez off Khan for three with the final ball of the 45th over and just three balls later Malinga was gone, bowled by Ajmal for a golden duck to leave Sri Lanka 195 for eight.
But Chandimal was showing some determination as he brought up his 50 off 62 balls leaving his team needing 18 off the final two overs before he and Mendis produced their big finish.
Earlier, Malinga returned figures of four for 57 and Suranga Lakmal three for 42 as Pakistan were dismissed for 232.
They won the toss and elected to bat but Malinga and Lakmal were quick to cause problems.
The latter had Ahmed Shehzad caught at mid-off by Priyanjan for 17 and Malinga removed fellow opener Sharjeel Khan, caught and bowled off a leading edge, for 18.
Malinga had his second victim when he had Sohaib Maqsood caught by Dilshan at point for seven, but Misbah-ul-Haq and Hafeez brought Pakistan back in the contest.
The duo put on 58 before Mathews, who conceded just 26 runs from his 10 overs, grabbed his only wicket of the innings by bowling Hafeez between bat and pad for 41.
Malinga took the key wicket of Misbah, whose 51 came from 74 balls and featured two fours and one six, as wicketkeeper Sangakkara held on to an inside edge.
That left Pakistan 169 for five and Malinga and Lakmal did not waste time in wrapping up the tail.
Ali offered some resistance with an unbeaten 41 but he ran out of partners as Pakistan's innings ended after 49.3 overs.