Edinburgh - 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, full back 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 scrum-half 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 try line. 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 flanker Hamish Watson -- was forced to depart.
The All Blacks had them backpedalling from a lineout 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 flanker 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 fly-half 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.
Scotland 17 (3)
Tries: Jonny Gray, Huw Jones
Conversions: Finn Russell (2)
All Blacks 22 (3)
Tries: Codie Taylor, Damian McKenzie, Beauden Barrett
Conversions: Barrett (2)
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
Substitutes: 16 George Turner, 17 Jamie Bhatti, 18 Simon Berghan, 19 Grant Gilchrist, 20 Luke Hamilton, 21 Henry Pyrgos, 22 Pete Horne, 23 Byron McGuigan
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
Substitutes: 16 Nathan Harris, 17 Wyatt Crockett, 18 Ofa Tu’ungafasi, 19 Liam Squire, 20 Matt Todd, 21 TJ Perenara, 22 Lima Sopoaga, 23 Anton Lienert-Brown