The All Blacks outscored their hosts three tries to two in Edinburgh on Sunday (NZ time) but were scrappy at times and were given a tough test by a side who are still to beat them in 33 meetings.
There was last-ditch hope for Scotland when Stuart Hogg broke free with just seconds remaining but first five-eighth Barrett arrived in cover to push the fullback into touch only a few metres from the line.
A try would have sealed at least a draw for the home, which they have only acheived once against the All Blacks, a 0-0 stalemate in 1964.
Scotland largely dominated the first half but were unable to capitalise and the teams went into the break locked together at 3-all.
New Zealand appeared to be moving away as Codie Taylor and Damian McKenzie scored tries in the opening 10 minutes of the second spell, before Jonny Gray responded for Scotland to close the gap back to five points.
A brilliant try from Barrett with 15 minutes to play restored his side's advantage to 12 but the hosts refused to go away and a Huw Jones try in the dying stages kept things interesting.
Scotland almost had a chance at a kick for a historic win when Hogg burst through, only for Barrett's timely intervention to save All Blacks blushes.
Handling errors cost Scotland a real chance of victory but they kept the All Blacks pinned back for good parts of the game, starting with a quick tempo and keeping up the pressure right to the end when the visitors had two players in the sin-bin.