Scotland coach Gregor Townsend says his side missed "the best opportunity in our history" to defeat New Zealand for the first time on Sunday.

A Stuart Hogg penalty try and Darcy Graham crossing over, coupled with a trio of penalties for Finn Russell, helped Scotland into a 23-14 second-half lead at Murrayfield.

But points from the boot of Jordie Barrett and a Jack Dempsey sin-bin allowed the All Blacks to fight back before Scott Barrett and Mark Telea bundled over to maintain New Zealand's streak in Scotland.

The All Blacks have not lost in 32 Tests against Scotland, stretching back 117 years, and Townsend acknowledged his team squandered a glorious opportunity after a 31-23 defeat.

"We're disappointed. There was so much to be proud of but we didn't see it through," he told Amazon Prime.

"It was a show of resilience after going 14-0 down to one of the best teams in the world, but we didn't get the win.

"You don't get to play New Zealand very often and that's the best opportunity in our history we've had to beat them."

The manner of defeat added greater frustration for Scotland, given they led 17-14 against New Zealand at half-time for just a third time in history, with the other two occurrences coming in 1905 and 1990.

Scotland captain Jamie Ritchie acknowledged the missed opportunity after the match, though he dedicated the performance to Doddie Weir, who was in attendance to mark five years since the launch of his charity that raises awareness of motor neurone disease.

"It's tough to be on the wrong side of the result. We were there or thereabouts for 70 minutes," Ritchie said. "It's some of the best rugby we've played over the last three weeks and I'm so proud of the players.

"We're glad we could put on a decent show for Doddie [Weir], just a shame we couldn't get the win. We spoke about being brave today and nothing defines brave more than Doddie."

Ian Foster had come under scrutiny for an underwhelming 2022 for the All Blacks, though praised New Zealand's fightback after they collected their sixth straight Test win.

"It was a tough game. I thought Scotland were tremendous, they put us under a lot of pressure," the All Blacks coach said. "Our bench was strong and that strength and composure was important.

"It was a high penalty count game and that allowed Scotland to get into our half too often, so we need to work on that.

"It would have been easy for us to panic in the last 15 minutes and [captain] Sam Whitelock deserves a lot of credit for keeping the boys calm."