New Zealand were within sight of their first Test victory in Sri Lanka in 14 years after 17 wickets fell on a thrilling penultimate day of the series on Wednesday.
The Black Caps set Mahela Jayawardene's side 363 for victory after first bowling them out for 244 and making 194 for nine declared in their second innings.
By stumps Sri Lanka had slumped to 47 for four as New Zealand dismissed Tharanga Paranavitana (0), Tillakaratne Dilshan (14), Kumar Sangakkara (16) and Jayawardene (5).
New Zealand went into the match on the back of five successive losses, their worst run in 57 years. Their last win in Sri Lanka was in 1998 and they were thrashed in the first Test of the series in Galle, losing by 10 wickets inside three days.
But thanks to fast bowlers Tim Southee, Trent Boult and Doug Bracewell, along with captain Ross Taylor, they set up the chance of a rare win.
"We are well aware of the history," coach Mike Hesson told reporters. "Results don't lie and five (losses) in a row is hard to take.
"I'm delighted we have been able to string together four good days but clearly we have still got a lot of work to do and tomorrow is a huge day for us.
"We have continued to bowl well and I thought we bowled well in Galle too with the young seamers and they stood up again tonight.
"It was nice to get the three big boys out (Dilshan, Sangakkara and Jayawardene) tonight but we know we are going to have to work for every one of those six wickets and it could take us until 5 o'clock (on Thursday)."
The day began with superb spells by Southee (five for 62) and Boult (four for 42) as New Zealand took the home side's final four wickets for 19 runs after they had resumed at 225 for six.
The stubborn Thilan Samaraweera top-scored with 76 but he failed to add to his overnight total before being caught at second slip by Martin Guptill off Boult to give the left-armer his 100th first-class wicket.
Boult also removed overnight batsman Suraj Randiv (39), lbw to an excellent inswinger, and claimed the final wicket when he had Rangana Herath (5) brilliantly caught in the gully by Kane Williamson.
Southee weighed in with the wicket of Nuwan Kulasekara (6), caught at first slip by Ross Taylor, to record his first five-wicket haul of the series.
That gave New Zealand a first-innings lead of 168 but when they slumped to 75 for five just after lunch, including the loss of three wickets in four balls, their dreams of levelling the series appeared on rocky ground.
They were rescued by Taylor, who followed up his first innings 142 with a patient 74 from 95 balls, including only two fours, the first of which he struck to bring up his half-century.
The captain found a willing ally in debutant Todd Astle, who made a resourceful 35 as the pair added 97 for the sixth wicket.
"There were some jitters straight after lunch," said Hesson. "It was important not to look too far ahead and Ross was extremely composed while Todd backed his own game to produce a crucial partnership for us."
Herath was again the most successful bowler for Sri Lanka and finished with three for 67 to give him nine wickets in the match and 20 in the two-Test series.
Sri Lanka were left with an awkward period to negotiate before bad light set in and in that time Southee (two for 19) and Bracewell (two for five) created havoc.
Southee removed Paranivitana first ball, lbw to a swinging delivery, and had Dilshan driving airily and edging to wicketkeeper Kruger van Wyk.
Then Bracewell got in on the act as Sangakkara played on before Jayawardene pushed tamely at another Bracewell delivery to edge to Van Wyk.
Samaraweera was unbeaten on one at the close with Angelo Mathews also on one and Sri Lanka needing a miracle to prevent a New Zealand victory.
New Zealand (1st innings): 412
Sri Lanka (1st innings) (overnight 225-6)
T. Paranavitana c Van Wyk b Southee 40
T. Dilshan b Southee 5
K. Sangakkara c Boult b Southee 0
M. Jayawardene c Williamson b Boult 4
A. Mathews c Guptill b Southee 47
T. Samaraweera c Guptill b Boult 76
P. Jayawardene c Williamson b Patel 12
S. Randiv lbw b Boult 39
N. Kulasekara C Taylor b Southee 6
R. Herath C Williamson b Boult 5
S. Eranga not out 3
Extras: (lb-3, w-1, nb-3) 7
Total: (all out; 94 overs) 244
Fall of wickets: 1-7, 2-7, 3-12, 4-102, 5-103, 6-128, 7-225, 8-232, 9-240.
Bowling: Southee 22-4-62-5 (1nb), Boult 21-7-42-4 (2nb, 1w), Patel 22-3-47-1, Astle 13-2-41-0, Bracewell 13-1-44-0, Williamson 3-1-5-0.
New Zealand (2nd innings):
M. Guptill c Dilshan b Eranga 11
B. McCullum st P. Jayawardene b Herath 35
K. Williamson c Paranavitana b Kulasekara 18
R. Taylor run out 75
D. Flynn lbw b Kulasekara 0
K. Van Wyk c Paranavitana b Herath 0
T. Astle c Dilshan b Randiv 35
D. Bracewell c Kulasekara b Herath 1
T. Southee not out 8
J. Patel st P. Jayawardene b Randiv 0
T. Boult not out 6
Extras: (lb-3, nb-2) 5
Total: (nine wickets dec; 54 overs) 194
Fall of wickets: 1-32, 2-56, 3-74, 4-74, 5-75, 6-172, 7-177, 8-180, 9-182.
Bowling: Kulasekara 12-2-47-2, Eranga 10-1-39-1 (1nb), Herath 21-3-67-3, Randiv 11-1-38-2 (1nb).
Sri Lanka (2nd innings):
T. Paranavitana lbw b Southee 0
T. Dilshan c Van Wyk b Southee 14
K. Sangakkara b Bracewell 16
M. Jayawardene c Van Wyk b Bracewell 5
T. Samaraweera not out 1
A. Mathews not out 1
Extras: (b-4, lb-6) 10
Total: (four wickets; 15.4 overs) 47
To bat: P. Jayawardene, N. Kulasekara, R. Herath, S. Randiv, S. Eranga
Fall of wickets: 1-0, 2-35, 3-41, 4-46.
Bowling: Southee 6-0-19-2, Boult 5-2-12-0, Bracewell 4-3-5-2, Patel 0.4-0-1-0.
Sri Lanka lead the two-match series 1-0.