Kumar Sangakkara scored his 10th Test double-century to leave Sri Lanka holding the upper hand against Pakistan in the first Test in Galle.
Sangakkara was finally dismissed for 221 in the evening session, stumped by Sarfraz Ahmed off Abdur Rehman having faced 425 balls and struck 24 fours, but his efforts helped Sri Lanka declare on 533 for nine - a lead of 82.
Rangana Herath then quickly dismissed Khurram Manzoor as Pakistan stumbled to four for one by the close on the fourth day.
Although a draw still looks the most likely outcome at Galle International Stadium, Sri Lanka could yet make a last-day push for victory, and that is largely thanks to Sangakkara.
His brilliant knock lifted the 36-year-old one clear of Brian Lara in the list of most Test double hundreds, with only Sir Don Bradman ahead of him with 12.
It also continued Sangakkara's remarkable record against Pakistan, which sees the left-hander averaging almost 85 from 20 matches, with three double tons and seven centuries - including an unbeaten 199 and 192.
He started the penultimate day on 102 with Sri Lanka on 252 for two in pursuit of Pakistan's 451 all out.
Sangakkara soon lost his overnight partner, Mahela Jayawardene only adding four to his 55 before he was trapped lbw by Junaid Khan, but Sri Lanka's record Test run-scorer was then joined by captain Angelo Mathews and they put on 181 for the fourth wicket.
Those two got Sri Lanka to within 13 of Pakistan's total before their stand was finally ended when Mathews fell for 91, becoming the first of five victims for spinner Saeed Ajmal, who took 5-166.
With the breakthrough made, Ajmal continued to make regular inroads into the Sri Lanka lower order with Kithuruwan Vithanage, Niroshan Dickwella and Dilruwan Perera all departing for five.
Sangakkara remained unmoved though, and he reached his 200 with a single off Ajmal in the 150th over - the same set of six that saw the dismissal of Perera.
Sangakkara and Dhammika Prasad added 36 for the eighth wicket to take Sri Lanka past the 500 mark before Sangakkara's stay was eventually ended midway through the final session.
The veteran batsman now has 11,886 Test runs, leaving him just 67 behind fifth-placed Lara on the list of all-time record run-scorers in five-day cricket.
Following Sangakkara's departure, Prasad helped add another 22 runs with Herath before the former became Ajmal's fifth victim, after which Mathews called his players in to leave Pakistan an awkward six overs before the close - during which time they lost opener Manzoor.