Tillakaratne Dilshan and Kumar Sangakkara each scored their second century of the series as Sri Lanka edged towards safety on day four of the second Test in Colombo.
Sri Lanka reached the close on 278-5 in their first innings, still 273 behind, with Sangakkara unbeaten on 144.
Dilshan was dismissed lbw by Junaid Khan for 121 shortly after tea, bringing an end to a second-wicket stand of 225.
And Pakistan hit back with three more wickets before stumps, Mahela Jayawardene and Thilan Samaraweera both going for ducks before nightwatchman Suraj Randiv (5) fell in the final over.
All three were lbw, paceman Junaid (3-56) accounted for Jayawardene, off-spinner Saeed Ajmal (1-77) trapped Samaraweera in front after 18 balls and left-arm spinner Abdur Rehman (1-50) saw off Randiv.
Resuming on 70-1 at the start of the day, Dilshan and Sangakkara were soon into the old routine, adding 65 runs during the morning.
A rain delay restricted the afternoon session to 16 overs but both batsmen were able to bring up their centuries shortly after tea.
Opening batsman Dilshan was first to the milestone from 178 balls with 11 fours and a six, his 14th Test hundred. He also became the eighth Sri Lankan to reach 5,000 Test runs during his innings.
And Sangakkara soon followed his partner into three figures, in his case for the 30th time in Tests, after facing 180 balls, including eight fours.
Sangakkara passed 2,000 runs at the Sinhalese Sports Club in the process and also became the highest run scorer against Pakistan, surpassing former India opener Sunil Gavaskar's aggregate of 2,089.
Junaid's double strike to remove Dilshan and Jayawardene started Pakistan's late fightback to revive their hopes of forcing a positive result heading into the final day.
Sri Lanka, who need another 74 to avoid the follow-on, lead the three-match series 1-0 after a 209-run win in the series opener in Galle. The third Test takes place in Pallekele from July 8-12.