Stockport County 2 Brentford 3
Goals from Jermaine Easter and Keith Briggs looked like they going to help Stockport spring a surprise against Martin Allen's men.
But Isaiah Rankin's 85th minute goal saw The Bees turn the tie on its head and saw the high-flying League One side progress.
Lloyd Owusu's 13th minute goal gave Brentford the lead against the run of play, before Easter equalised and a Briggs header saw Stockport ahead early into the second half.
However, Dudley Campbell was on target in the 65th minute to level matters and then some slack defending saw Rankin gave the visitors victory at the death.
Defeat was rough justice on County, a side rooted to the foot of the league table and many a mile away from Brentford, third and going strong in their respective division.
Briggs kept Brentford keeper Stuart Nelson on his toes with an ambitious volley in the sixth minute, before fellow midfielder Danny Boshell fired over four minutes later as the home side pressed for an early opener.
Matt Hamshaw saw a powerful shot deflected over the crossbar as Jim Gannon's men had the better of the play, before an error from young defender Michael Raynes allowed Owusu to score against the run of play.
The striker took full advantage of Raynes' mistake, firing a right-footed half-volley into the left-hand corner to beat a despairing dive from James Spencer.
But County were level five minutes later courtesy of a splendid finish from Easter.
Briggs had seen a shot blocked by Nelson but, still under pressure, Brentford were caught napping by a precision pass from Damien Allen.
The midfielder's ball arrived at the feet of Easter and, after a burst of pace, he fired home into the left corner.
Easter hit the bar before the half closed, but County went ahead three minutes after the interval when Briggs headed in a Hamshaw corner from the left wing.
However, Campbell's controversial equaliser, which County debated had not crossed the line, saw the visitors fight on, and Rankin's strike five minutes from time secured a fourth round spot.