Angelo Mathews and Kusal Mendis batted the entire day and scored unbeaten hundreds as Sri Lanka emerged from the jaws of defeat to thwart New Zealand on day four of the opening Test.

A comprehensive victory appeared to be a foregone conclusion for New Zealand after visiting Sri Lanka was reeling at 20/3 when play resumed in Wellington on Tuesday.

But, Mathews (117 not out) and Mendis (116 not out) frustrated the Black Caps by scoring tons at Basin Reserve, and Sri Lanka reached 259/3 by stumps without losing a wicket to be only 37 runs adrift.

The pair's 246-run stand has given Sri Lanka hope it can survive and avoid defeat in the first Test after an unbeaten 264 from Tom Latham on Monday led New Zealand to a mammoth 578 total — a lead of 296 runs.

Sri Lanka — which was bowled out for 282 in the first innings — was outclassed over the first three days but the tourist produced a timely fightback thanks to Mathews and Mendis.

Mathews and Mendis showed plenty of grit, determination and poise against a New Zealand bowling attack devoid of ideas, with Black Caps skipper Kane Williamson struggling for answers.

Neil Wagner (0/100), Tim Southee (2/36) and Trent Boult (1/50) were easily dealt with by Mathews and Mendis, who looked comfortable throughout against Colin de Grandhomme (0/24), Ajaz Patel (0/46) and Jeet Raval (0/1).

Mendis was impressive for his sixth Test hundred and second outside of Asia, hitting regular boundaries before adopting a more reserved approach following lunch.

The 23-year-old, who only scored 38 runs off 92 after the break, hit a costly Wanger for four as he brought up a confidence-boosting ton.

Mathews, meanwhile, survived an onslaught and grinded his way to a ninth century late in the day, which he celebrated by doing push-ups in the middle of the ground.