The Kangaroos took the early initiative in Sydney with a ninth-minute try from Brent Tate and three kicks from scrum-half Johnathan Thurston gave them a 10-2 advantage.
But the Kiwis cut the gap with a 28th-minute try from replacement Frank Pritchard and levelled after a break from Iosia Soliola.
Thurston’s goal put Australia back in the driving seat but Stacey Jones levelled to set up a tense finish and extra-time after a 12-12 tie.
And Lockyer capped a fantastic year with the winning try in the second half of extra-time after Thurston made the break and found his skipper in support.
After performing a rousing haka in front of a packed Aussie Stadium, the Kiwis got off to a poor start by conceding two penalties at the play-the-ball, and Thurston duly opened the scoring by kicking a 35-metre penalty.
However, the Kangaroos then conceded a penalty themselves allowing Jones to level the scores with a straightforward kick from 10 metres.
Australia then claimed the first try of the match, although it involved a slice of luck.
Petero Civoniceva’s pass went to ground on halfway and inadvertently found its way to Nathan Hindmarsh, which caught Jones out of the defensive line.
Hindmarsh laid the ball off to Andrew Ryan, who drew Steve Matai before passing to Mark Gasnier and Tate applied the finished touch.
Thurston kicked the extras from the touchline to make it 8-2 and the Kangaroos kept the pressure on, forcing a goalline drop-out before Justin Hodges broke free on the left only to be stopped short on the last tackle.
The Australian pressure resulted in the Kiwis committing another penalty at the play-the-ball in the 23rd minute and Thurston stepped up again to convert from 20 metres.
Yet with the Kiwis seemingly struggling to get a foothold in the game, star half-back Jones brought them back into it with a piece of genius in the 28th minute. He put a chip kick over the Australian line, which Leeds Rhinos-bound full-back Brent Webb collected on the full before offloading for Pritchard to score.
Jones missed a difficult kick from the touchline but the Kiwis had a great chance to draw level in the 33rd minute, only for winger Manu Vatuvei to knock-on within 10 metres of the Australian line.
Gasnier then found a gap 30 metres short but the Australian centre lost his footing at a crucial moment.
Jones almost created another try just before the half-time hooter with a sideways kick under the posts but Thurston caught bravely with a number of Kiwis bearing down on him.
New Zealand finally got themselves level in the second half after winning a penalty and taking advantage in style.
Jones’ vision saw the ball reach Nigel Vagana whose handiwork found the run of Soliola, enabling the Sydney Roosters man to cross on his home ground and level the scores.
Jones missed the kick as the scores briefly remained level but minutes later Thurston was able to restore Australia’s lead with the boot after the Kiwis conceded a sloppy penalty.
With the game delicately poised at 12-10 mistakes began to creep in as both sides clearly began to tire.
New Zealand were gifted a lifeline by Hindmarsh when he clearly obstructed Jones, who was attempting to rush through after a kick. Jones kicked the penalty to tie the scores at 12-12 and set up a thrilling final 10 minutes.
Jones went for a winning kick with six minutes left and Webb did likewise soon after but also missed.
The drama continued as Thurston took his turn to miss a drop goal chance with two minutes to go.
Willie Mason’s power enabled him to break through and when he kicked down the line intelligently, Greg Inglis got to the ball first but it bounced off his shoulder. Thurston subsequently grounded the ball but the video referee judged Inglis had knocked the ball on.
As the game went to golden-point extra-time both sides drove forward looking for the all-important breakthrough. It came when Thurston made the half break and fed the supporting Lockyer who made it over the line despite a desperate tap tackle attempt.
New Zealand trio Jones, Ruben Wiki and Nigel Vagana all announced their retirements from international action after the game.
Wiki made his intentions to retire known straight after the match, before Kiwi management confirmed Jones and Vagana had also played their last games for their country.