Following in the footsteps of openers Abid Ali and Shan Masood, Azhar Ali and Babar Azam smashed centuries before Pakistan declared their second innings on 555-3.
Chasing 476, Sri Lanka were 212-7 at stumps with Oshada Fernando waging a grim battle with his maiden test century after being let down by the top order.
Fernando was batting on 102, having studded the gutsy knock with 13 boundaries, with the tourists staring at a series defeat at Karachi's National Stadium.
One-to-watch Bess Heath on her unconventional upbringing and her burgeoning cricket career
The two-match series, which began with a drawn test in Rawalpindi, marks Pakistan's first tests on home soil since the 2009 militant attack on Sri Lanka's team bus in Lahore.
Resuming day four on 395-2, Azhar and Babar joined the run-fest as, for only the second time in international cricket, each of the top four batsmen got a hundred in the same innings.
Babar was given lbw on 33 but he reviewed the decision to prolong his knock after replays confirmed an inside edge.
Azhar hit Vishwa Fernando for three consecutive boundary, while Babar meted out a similar treatment to Lahiru Kumara.
Azhar cut Lasith Embuldeniya to bring up his 16th test hundred and made 118 before the left-arm spinner dismissed him stumped.
Babar took a single off Kumara to bring up his second hundred of the series and third in the last four tests.
Pakistan went at nearly six-an-over in the morning session before declaring at lunch.
Needing to bat out five sessions to save the test, Sri Lanka's top order meekly capitulated to be 97-5.
Fernando and Niroshan Dickwella, who made 65, resisted with their 104-run stand for the sixth wicket but it may not be enough to save the match. (Reporting by Amlan Chakraborty in New Delhi; editing by Christian Radnedge)
Virus-hit India to help move Australian IPL cohort to Sri Lanka or Maldives
Ex-Australia spinner MacGill kidnapped for ransom - reports