Earnshaw opened the scoring with a glorious free-kick
Sunderland survived a major scare before seeing off Championship side Nottingham Forest to progress to the third round of the Carling Cup.
Striker Robert Earnshaw put Forest ahead with a wonderful curling free-kick that found the top corner.
Sunderland were heading out until Phil Bardsley steered in an El-Hadji Diouf cross in the dying stages.
Substitute David Healy got the winner and his first Sunderland goal with a sidefooted finish in extra-time.
The home side made sure it was a difficult return for ex-Forest star Roy Keane with a determined display against their Premier League opponents.
The Sunderland boss had lost his previous three cup games since taking charge and will be mightily relieved to leave the City Ground with the victory.
There were warning signs from early on that the Black Cats would not have it all their own way when Nyron Nosworthy needed a last-ditch tackle to prevent a tap-in for Earnshaw.
Forest grew in confidence and enjoyed the bulk of the ball, although Sunderland were still able to carve out the better chances.
Djibril Cisse struck a left-foot shot that was headed for the bottom corner before a vital Smith touch took it on to the post.
Daryl Murphy headed just wide and Dean Whitehead saw a first-time shot saved by Smith after being put clean through.
Forest began to turn possession into scoring opportunities after the break.
Matt Thornhill clipped in a cross for Earnshaw but he could only get the deftest of touches as the ball drifted wide.
A few minutes later Earnshaw atoned for that miss when he won a free-kick on the edge of the Sunderland box.
The Welsh forward produced a moment of class to curl it around the wall and past a stranded Craig Gordon.
Sunderland's players looked frustrated, while Keane fumed on the sidelines.
Ex-Forest favourite Andy Reid volleyed into the side-netting as time looked to be running out on the visitors' cup hopes.
Sunderland's leveller came somewhat out of the blue - and from an unusual source in Bardsley.
It was hard luck on the impressive Smith, who brilliantly kept out Liam Miller's shot but saw Diouf fire the ball back into the centre for Bardsley to score his first goal in English football.
Thornhill nearly restored Forest's lead in the last minute but headed off target when unmarked.
Three minutes into extra-time Cisse found the fresh legs of Leadbitter, who worked his way into a shooting position only to see his effort saved by Smith.
But Healy was in the right place to follow up and sidefoot the rebound into an empty net.
A tiring Forest had one last chance to take the game to a shoot-out but Chambers' header from Earnshaw's free-kick struck the bar.
Sunderland boss Roy Keane told BBC Radio Newcastle:
"It was a good Cup tie, credit to Forest, they made it very hard for us, but credit to my lads for getting us back in and getting us through.
"This time last year we were out of the Cup so plusses and minuses. The minus would be the injuries and the fact that we can play a lot better, the plusses would be that we showed good character, scored a great goal and we created chances in the first half."
Bookmark with:
What are these?