Williamson’s unbeaten 242, with Watling not out 142, rescued New Zealand from a deep hole and carried them to 524-5 declared in their second innings.
Sri Lanka were left facing a 390-run final-day target on a ground where the record for a successful fourth innings chase is 277-3 set by Pakistan 11 years ago.
At stumps they were 45-1 with Kaushal Silva on 20 and Dhammika Prasad on one.
Dimuth Karunaratne was out for 17 four overs before the close, the only dismissal of the day.
With wickets hard to come by on a flat track, and Sri Lanka holding middle-order strength led by Kumar Sangakkara, a draw seemed the likely outcome. This would give New Zealand the two-Test series 1-0.
Williamson and Watling came together as New Zealand’s last recognised batsmen with the score on 159-5, a perilous 24-run lead. They methodically ground the Sri Lankan bowlers into submission over the following eight hours.
Williamson said their focus was on occupying the crease to save New Zealand and records were secondary.
“We needed to draw on that to bat as long as we did. That was very much the motivating factor. Talk about 100s or 200s, it’s great to get them but more important to keep going to put the team in a better position,” he said.
Sri Lanka need a further 345 on the final day to win. While the odds favour New Zealand, Williamson said both sides fancied a result.
“We know if we put it in the right areas and bowl the way we know we can, we can create opportunities, but anyone with someone like Kumar Sangakkara in their team will certainly have confidence, and it’s set up for a really good day’s cricket.”
New Zealand declared 13 runs ahead of the previous sixth-wicket record of 352 set by Watling with Brendon McCullum in similar Test-saving circumstances against India a year ago.
Then, as now, New Zealand were fighting to claim the series after winning the first Test.
Sri Lanka squandered several opportunities to break the partnership and regain control, and paid a heavy price for sloppy fielding.
Williamson offered four chances — dropped by Rangana Herath on 29, dropped by Nuwan Pradeep on 60 and dropped by Prasanna Jayawardene on 104 before Jayawardene missed a blatant stumping opportunity on 233.
Williamson’s unbeaten 242, the product of more than 10 hours at the crease, gave the 24-year-old his ninth Test century and the seventh highest score by a New Zealander.
After cautiously steering New Zealand into a commanding position, Williamson opened up towards the end of the innings, scoring 52 of the 79 runs added in the 15 overs before the declaration.
In his marathon innings he faced 438 deliveries and scored 18 fours, including a boundary to midwicket when he danced down the wicket to bring up his maiden 200 off Rangana Herath.
While Williamson steered New Zealand to safety, Watling proved a sound ally at the other end as he brought up his fourth Test century. He had nine fours and a six in his 142.
His previous best was 124 in his match-saving knock with McCullum against India.
As the innings progressed the Sri Lankan captain Angelo Mathews set defensive fields in hopes the batsmen would make a mistake, but when the chances came they were not taken.
Herath, the leading bowler in the world last year with 60 wickets at 24.85, finished with figures of one for 154.
Sri Lanka’s most successful bowler Pradeep had figures of three for 117 after being three for 32 before Watling went to the middle. (AFP)