A moment of New Zealand magic denied Scotland a famous victory in a pulsating contest at BT Murrayfield.
With the game firmly in the balance, Sonny Bill Williams broke through the midfield before offloading to Damian McKenzie, who fed Beauden Barret to maintain the All Blacks' hold over Scotland with a 22-17 win.
Stuart Hogg made a brilliant dart for the line in the dying moments but was tackled metres short to close out a brilliant performance from Gregor Townsend's side.
New Zealand have never lost to the Dark Blues, and that piece of history looked under threat after Jonny Gray's try pulled the scores back to 15-10 at one point in the second half, but Barrett's try put them out of sight despite Huw Jones giving the home crowd fresh hope.
There was a real buzz around the ground prior to kick-off as the teams lined up for their respective anthems, and the atmosphere inspired the hosts to a fine start.
Townsend emphasised the need to play with intent and aggression prior to the match, and his side started on the front foot and did not shy away from throwing the ball about.
Hogg made a couple of early breaks but it was the boot of Finn Russell that put the hosts into an early, and deserved, lead.
However, the All Blacks grew stronger as the half wore on, and went into the break level as John Barclay went in off his feet at the breakdown and gave Barrett the chance to kick a simple three points from the 22 as a hard-fought first half came to a close with the sides level.
For all their hard work in the first 40 minutes, Scotland were dealt a harsh blow at the start of the second half through some slack defence.
Ali Price let Rieko Ioane claim a high ball before a poor tackle allowed Codie Taylor to gallop free down the wing. The ball was recycled and several phases later made its way back out to Taylor, who was free on the wing to touch down.
Scotland were forced into a third substitution with Luke Hamilton suffering a nasty ankle injury on debut, and that disrupted any rhythm they had, and the All Blacks pounced as Williams kicked through for McKenzie to dive on over the line to open up a 12-point lead.
However, the hosts showed good character to hit back straight away, working their way up to the New Zealand try-line through a strong lineout, and Sam Crane was sent to the bin before Gray stretched over the line to breathe life back into the match.
With Scotland full of renewed hope, it took a moment of set-piece brilliance from New Zealand to effectively end the game as Barrett raced in to re-establish the 12-point gap.
Wyatt Crockett replaced Crane in the bin as Scotland again hit back heroically. They appeared to have missed their opportunity after building up pressure in the 22, but Jones raced in to make for an exciting finish.
Hogg made a last-gasp, brilliant burst for the line but knocked on metres out to end a pulsating second period.
Scotland v New Zealand - Match Highlights
Scotland v New Zealand - Match Stats
Scotland: (3) 17
Try: Gray (62'), H Jones (77')
Conversion: Russell (63', 78')
Penalty: Russell (7')
New Zealand: (3) 22
Try: Taylor (45'), McKenzie (51'), Barrett (67')
Conversion: Barrett (52', 69')
Penalty: Barrett (39')
Man of the Match: Stuart Hogg (Scotland)