Isaiah Rankin struck six minutes from time to send Brentford into round four for the second year in a row.
Rankin turned in a cross from fellow substitute Andy Frampton to settle a thrilling end-to-end Cup tie.
Lloyd Owusu drove Brentford ahead from 20 yards but Jermaine Easter quickly raced clear to equalise.
Keith Briggs headed home a corner to put League Two County ahead but Dudley Campbell scrambled the ball over the line to level before Rankin won it.
Managerless Stockport, bottom of the League, made the brighter start and Easter and Briggs both came close before they were caught on the counter-attack.
A goal-kick from Bees keeper Stuart Nelson found Owusu, who capitalised on a mistake by defender Michael Raynes and lifted the ball over keeper James Spencer to put the visitors ahead.
County's pressure paid off however when they levelled four minutes later as Easter collected Briggs' pass and slotted home past Nelson.
Easter then crashed a shot against the Brentford bar after good work by Matty Hamshaw before Campbell wasted a chance to restore the visitors' lead just before half-time.
After Briggs scored from Hamshaw's corner Brentford hit back and after Owusu twice went close, Campbell forced in the equaliser which was awarded by the linesman.
The visitors stayed on top and Rankin, a hero in the Bees' run to the fifth round last season, grabbed the winner from close range.
County poured forward in the closing stages and keeper Spencer joined the attack but they could not find another goal much to the despair of caretaker boss Jim Gannon.
Stockport: Spencer, Clare, Raynes, Ashley Williams, Robinson, Allen, Boshell (Wolski 69), Briggs (Le Fondre 70), Singh, Hamshaw, Easter.
Subs Not Used: Dje, Tunnicliffe, Crowe.
Goals: Easter 17, Briggs 48.
Brentford: Nelson, O'Connor, Sodje, Turner, Tillen, Brooker (Rankin 59), Newman, Hutchinson, Gayle (Frampton 69), Owusu, Campbell.
Subs Not Used: Mousinho, Lewis, Bankole.
Goals: Owusu 13, Campbell 65, Rankin 84.
Ref: K Wright (Cambridgeshire).