Thisara Perera's brisk half century for Sri Lanka left Pakistan struggling to catch the home side after three days of the third and final cricket Test on Tuesday.
Opening batsman Tharanga Paranavitana (75) and Thilan Samaraweera (73) powered Sri Lanka to match Pakistan's first-innings score, then Perera's late whacks lifted their total to 337 all out, a lead of 111.
In the half hour to stumps, Pakistan lost Taufeeq Umar (4) and were 27-1, trailing by 84 and needing a win to tie the series.
TV replays suggested Nuwan Kulasekera pitched the ball outside Umar's leg stump, but Umar did not have the benefit of the Decision Review System to make a referral.
Paranavitana and Samaraweera shared a fourth-wicket stand of 143 before Perera's onslaught when Pakistan took the second new ball after tea.
Sri Lanka were ahead by just 4 when Perera, batting at No. 8, was joined by Kulasekera (33).
Pakistan first innings 226
Sri Lanka first innings
T Paranavitana b Ajmal 75
D Chandimal lbw b Junaid Khan 8
K Sangakkara b Junaid Khan 0
M Jayawardene lbw b Sami 12
T Samaraweera lbw b Ajmal 73
A Mathews c Shafiq b Junaid Khan 9
P Jayawardene lbw b Gul 20
T Perera b Junaid Khan 75
N Kulasekara c Umar b Junaid Khan 33
R Herath lbw b Ajmal 2
D Fernando not out 0
TOTAL: (all out) 337
Fall of wickets: 1-14, 2-14, 3-44, 4-187, 5-200, 6-204, 7-236, 8-320, 9-337.
Pakistan second innings
M Hafeez not out 8
T Umar lbw b Kulasekera 4
A Ali not out 6
TOTAL: (for one wicket) 27
Fall of wicket: 1-16.
The pair shared a Sri Lanka-record 84 runs for the eighth wicket against Pakistan. It bettered the 76 between Aravinda de Silva and Chaminda Vaas made at Colombo's Sinhalese Sports Club in 1997.
Perera rode some luck as he was dropped on 11 and 22 in scoring 75 off 86 balls with four boundaries and three sixes.
Umar Gul dropped a hard caught and bowled chance and captain Misbah-ul-Haq missed a low catch in the slips before the left-hander cut loose.
Perera smashed Gul for two boundaries and a six-in-one over before hoisting offspinner Saeed Ajmal (3-66) for two big sixes as sunlight bathed Pallekele Stadium for the first time in three days.
Pakistan left-arm seamer Junaid Khan (5-70) was rewarded for his untiring effort with a five-wicket haul when he clean-bowled Perera.
Earlier, after the second day's play was washed out, Paranavitana and Samaraweera dug in well in the first session and guided their team to 142 after resuming at 44-3.
Khan, who took two late wickets on the first day in the space of four deliveries, and Gul Tested Paranavitana and Samaraweera early on Tuesday before the batsmen settled in.
Paranavitana was struck on the helmet by Mohammad Sami's short-pitched delivery, but it did not shake the confidence of the left-handed opener as he raised his half century with a punched drive to cover boundary off part-time seamer Younis Khan.
Samaraweera got a lucky escape just after the break when Younis Khan dropped a straightforward catch in the second slip off Gul as the batsman attempted to drive.
Samaraweera added salt in the wounds of Gul when he drove the seamer to the cover boundary in the next over to complete his 50.
The players left the field for 20 minutes because of rain and the little break favoured Pakistan when play resumed.
Ajmal bowled round the wicket and had Samaraweera leg before wicket off a delivery that didn't spin much. Samaraweera hit nine fours in his 160-ball knock.
Khan's superb seam and swing bowling saw Angelo Mathews giving a sharp catch to Asad Shafiq in the gully before Paranavitana chopped Ajmal back onto his stumps. Paranavitana, who struggled in the first two Tests and was on the verge of being dropped from the side, hit eight fours and faced 188 balls.
Pakistan were without wicketkeeper Adnan Akmal, who suffered a hairline fracture in his hand while facing Dilhara Fernando's short delivery on the first day. Umar replaced Akmal behind the wickets.
Sri Lanka won the first Test by 209 runs at Galle before the second Test ended in a draw on a placid wicket at Sinhalese Sports Club in Colombo.