It took an injury-time tackle from New Zealand flyhalf Beauden Barrett on Scotland's flying fullback Stuart Hogg to deny a dogged Scotland the chance of an historic victory against the All Blacks on Saturday.
A converted 76th-minute try from centre Huw Jones had put the Scots within five points of the visitors and another converted try would have secured their first ever triumph against the men in black.
As it was, second-half tries from hooker Codie Taylor, fullback Damian McKenzie and Barrett, plus two conversions and a penalty from Barrett proved enough to win 22-17 and preserve the All Blacks' unbeaten record against Scotland, who were level 3-3 at the interval.
Scotland head coach Gregor Townsend acknowledged the challenge of beating the back-to-back world champions as "the ultimate test" but urged his players to impose themselves from the start.
They had an excellent chance to lay down an early marker but lost possession from a catch and drive and then prop Darryl Marfo spilled the ball when Hogg launched a potentially threatening attack.
Still, the home side were rewarded for their endeavour when Russell landed his first penalty attempt to give them a 3-0 lead after six minutes.
They might have doubled the advantage seven minutes later but Russell pushed wide a long-range penalty.
Russell went for a line-out drive rather than the posts with the next Scottish penalty award but, after the forwards had driven to within five metres of the line, centre Jones dropped the ball as it was shipped wide.
It took the All Blacks 20 minutes to get into the Scotland 22. They got perilously close to the home whitewash but lock Sam Whitelock, and then centre Ryan Crotty, were thwarted by the resolute home defence.
New Zealand were fortunate to still have 15 men on the field when Hogg was tackled in the air by the big Highlanders wing Waisake Naholo.
After consulting TMO David Grashoff, however, referee Matthew Carley decided the offence was worthy only of a penalty, as Scotland scrumhalf Ali Price had been in between the two players when the All Black made his move to engage in the tackle.
Barrett stepped up to kick a 38th-minute penalty to tie the half-time scores at 3-3.
Scotland began the second half on front foot with centre Alex Dunbar stopped a metre or two short of the tryline. But after 44 minutes Townsend's team found themselves behind.
The All Blacks conjured a try from nothing, Taylor taking a long, looping pass from Barrett to finish a simple overlap on the left, the Crusaders hooker having launched the attack himself.
Barrett was off target with the conversion but made amends seven minutes later.
Scotland were getting to grips with a re-shuffle of their forwards at the time after replacement prop George Turner had to shift to the blindside flank when Luke Hamilton – himself a replacement for the injured openside flank Hamish Watson – was forced to depart.
The All Blacks had them backpedalling from a line-out drive and centre Sonny Bill Williams dinked a grubber kick through for McKenzie to score.
Barrett's conversion made it 15-3 but Scotland turned the tide. The All Blacks were reduced to 14 men on the hour mark when persistent infringing close to the line prompted a yellow card for flank Sam Cane.
A minute later Gray scored from a close-range ruck and Russell converted, pulling the score back to 15-10.
But then, after 66 minutes, Williams punched a hole up the middle, slipped the ball out of the back of his hand to McKenzie and the full back fed in Barrett for a try on the right. The flyhalf converted and the world champions had a 22-10 cushion.
Just as Cane returned to the field, replacement prop Wyatt Crockett departed to the sin bin and, once again the Scots fought back.
Four minutes from time Hogg threated a grubber kick through for centre Jones to score, Russell's conversion making it 22-17.
Man of the match: Stuart Hogg was unbelievable during the 80 minutes. He was dangerous with ball in hand and he was just a joy to watch.
Moment of the match: Beauden Barrett's try was magical.
Villain of the match: Nobody, it was a good game.
Tries: Gray, Jones
Cons: Russell 2
For New Zealand:
Tries: Taylor, McKenzie, Barrett
Cons: Barrett 2
Yellow cards: Sam Cane (New Zealand, 61 - cynical play, slowing the ball); Wyatt Crockett (New Zealand, 71 - cynical play, tackling the player without the ball)
Scotland: 15 Stuart Hogg, 14 Tommy Seymour, 13 Huw Jones, 12 Alex Dunbar, 11 Lee Jones, 10 Finn Russell, 9 Ali Price, 8 Cornell du Preez, 7 Hamish Watson, 6 John Barclay (captain), 5 Jonny Gray, 4 Ben Toolis, 3 Zander Fagerson, 2 Stuart McInally, 1 Darryl Marfo.
Replacements: 16 George Turner, 17 Jamie Bhatti, 18 Simon Berghan, 19 Grant Gilchrist, 20 Luke Hamilton, 21 Henry Pyrgos, 22 Pete Horne, 23 Byron McGuigan.
New Zealand: 15 Damian McKenzie, 14 Waisake Naholo, 13 Ryan Crotty, 12 Sonny Bill Williams, 11 Rieko Ioane, 10 Beauden Barrett, 9 Aaron Smith, 8 Kieran Read (captain), 7 Sam Cane, 6 Vaea Fifita, 5 Samuel Whitelock, 4 Luke Romano, 3 Nepo Laulala, 2 Codie Taylor, 1 Kane Hames.
Replacements: 16 Nathan Harris, 17 Wyatt Crockett, 18 Ofa Tu'ungafasi, 19 Liam Squire, 20 Matt Todd, 21 Thomas Perenara, 22 Lima Sopoaga, 23 Anton Lienert-Brown.
Referee: Matthew Carley (England)
Assistant referees: Romain Poite (France), Ian Davies (Wales)
TMO: Graham Hughes (England)