The relegation-threatened Merseyside club had slightly the better of a first half lacking in good football, with captain Ian Thomas-Moore forcing the save of the half from Brentford goalkeeper Wojciech Szczesny.
But they fell behind early in the second when a Kevin O’Connor shot flew through a crowd of bodies and nestled in the corner with 49 minutes gone.
Brentford could have sewn it up when Myles Weston curled a shot inches wide but Joss Labadie levelled when his free-kick came back off the wall and he fired it home after 62 minutes.
Brentford got their winner with four minutes left when Charlie MacDonald set up Ben Strevens to score from close range.
MacDonald almost added a third with a low shot while Szczesny made a fine late save to prevent Marlon Broomes heading an equaliser for Rovers.