Set 285 to chase, Sri Lanka needed 16 off the last two overs and four off the final, with No.10 batsman Sachitra Senanayake driving Shahid Afridi for a boundary to seal a tense win for his team with two balls to spare.
The win levelled the five-match series at 1-1 after Pakistan won the first match by 11 runs in Sharjah on Wednesday.
Ahmed Shehzad smashed a career best 124 off 140 balls for his fourth one-day century as Pakistan posted a challenging 4-284 in 50 overs after being sent in to bat.
Sri Lanka needed 44 off the last six overs as Angelo Mathews (47) and Dimuth Karunaratne (16 not out) held their nerve during a match-turning 44-run stand for the eighth wicket.
Sri Lanka had set themselves up nicely at 2-159 with Kumar Sangakkara (58) and Dinesh Chandimal (44) before Mathews came into his own.
When on 29, Sangakkara completed 12,000 one-day runs in his 359th match. He is the fourth batsmen to score 12,000 or more runs in the one-day chart led by now retired Sachin Tendulkar of India (18,426 in 463 matches) followed by Australian Ricky Ponting (13,704 in 375) and Sri Lankan Sanath Jayasuriya (13,430 in 445).
Sangakkara reached his 82nd half-century off 54 balls but eight runs later played Ajmal onto his stumps. In all he hit four boundaries and a six off 67 balls.
Sri Lanka were off to a swift 48-run start by Tillakaratne Dilshan (40) and Kusal Perera (16) before both fell run out to leave Sri Lanka 2-65.
With the ball hard to grip for the spinners as dew fell, Sangakkara and Chandimal added 94 for the third wicket.
Nuwan Kulasekara chipped in with a 33-ball 32 with two sixes and three fours to up the scoring in the final overs.
Earlier, Shehzad steadied Pakistan's innings during a 76-run second wicket stand with Mohammad Hafeez (32).
Shehzad also added another 105 runs for the fourth wicket with skipper Misbah-ul-Haq (59 not out).
The remaining matches will be played in Sharjah (Sunday) and Abu Dhabi (Wednesday and Friday).