Saud Shakeel became the first Pakistan batter to make a double hundred in Sri Lanka as the visitors took control of the first test after making 461 on Tuesday.
Behind by 149 runs on the first innings, Sri Lanka reduced that to 135 when it was 14 for no loss by stumps on day three.
Shakeel was stranded on 208 not out, his second century in two tests, when Pakistan was all out after tea.
In six tests since his debut in December, he has scored a double hundred, a hundred and five half-centuries. He has a Bradmanesque average of 98.5.
Shakeel was the mainstay of Pakistan's innings by stitching valuable partnerships with the lower middle order and the tail after the top order collapsed.
At 101-5, a first-innings lead was the last thing on Pakistan's mind, but Shakeel added 177 runs with Agha Salman to rescue his side. The partnership was a record for Pakistan for the sixth wicket against Sri Lanka, improving on the 173-run stand between Sarfaraz Ahmed and Asad Shafiq in Dubai in 2017.
With an unorthodox batting technique, Shakeel used his feet expertly to the spinners, punishing the loose balls and forcing the Sri Lankans to spread the field during his stand with Salman.
Ramesh Mendis made the breakthrough in the rain-affected morning session when he had Salman stumped for 83.
Batting with the tail, Shakeel changed gears, quite content to farm the strike from the tail and the tactic worked.
After lunch, Shakeel extended the seventh-wicket stand with Noman Ali to 52 runs. He added a match-defining 94 runs with Naseem Shah for the ninth wicket. Naseem's contribution was just six runs.
Sri Lanka was sloppy in the field as Shakeel was dropped twice, on 93 by Nishan Madushka at leg gully and on 139 by Angelo Mathews at deep midwicket.
The left-handed batsman reached his double hundred cutting Dhananjaya de Silva behind square, bisecting the fielders for four.
When the last man Abrar Ahmed was dismissed for 10 with a total of 461, Shakeel was unbeaten after a marathon innings lasting for 506 minutes, in which he faced 361 balls and hit 19 boundaries.
Only two overseas players — West Indies' Chris Gayle (333) and England's Joe Root (228) — have higher innings in Galle than Shakeel.
Ramesh Mendis finished with a five-wicket haul, his fifth in tests, while Prabath Jayasuriya claimed three wickets. Overall, the Sri Lanka bowlers struggled against the clever batsmanship of Shakeel.
Sri Lanka's openers faced 3.4 overs without trouble before bad light forced an early end to play. But they have a huge ask on their hands on day four.