Farid El Alagui came off the substitutes’ bench to score twice and book Brentford a place in the Capital One Cup Second Round.
The Bees looked as if they may be on the end of an upset, or at least face extra time, against Sky Bet League Two Dagenham and Redbridge as they trailed at the break and were all square with 90 minutes on the clock.
But El Alagui was thrown on to make his first competitive appearance at Griffin Park since he was taken off on a stretcher with a serious knee injury against Crawley Town ten months ago.
He levelled it up and then scored the winner to give The Bees a 3-2 victory in the dying seconds.
Brentford started with Stuart Dallas as part of a midfield three and although the home side had the better of the game, Dagenham were well-organised and compact, which made it hard for The Bees to create much.
Jake Reeves shot wide when a corner was half cleared but the game meandered to the quarter hours mark with Brentford on top but not in command.
The visitors were growing in confidence and getting a territorial foothold in the game but it was still a shock when they went ahead.
Josh Scott came in off the right and fired in a shot that was blocked, when he put the ball back in, Ben Nugent turned the ball past Jack Bonham and in to the net.
Assistant referee Ian Crouch had his flag raised to indicate Rhys Murphy was offside behind Nugent but referee Pat Miller decided the striker was not interfering, despite the defender only needing to play the ball because of the Dagenham man’s position, and gave the goal.
Jake Reed shot wide soon after for the visitors, who noticeably grew in confidence, but Dallas was moved to the right as the game reached the half hour mark and Brentford improved markedly.
Martin Fillo slotted in to a role just behind Paul Hayes and Brentford started to play their football, much of it going through the Czech Republic international or Dallas on the wing.
Dallas shot wide for Brentford before Sam Saunders cut inside and fired wide and Emmanuel Oyeleke missed the target when a corner was half cleared.
Murphy could have doubled the lead when he shot wide from the edge of the box after getting in behind and on to a Scott pass but all the action was at the other end.
Saunders shot over before Chris Lewington held on to an Oyeleke chip and Dallas shot over from the right angle.
A Paul Hayes shot deflected to Lewington, Oyeleke fired narrowly wide and Saunders fired a free kick in to a defensive wall before Dallas blasted the loose ball over.
The Daggers almost doubled their lead at the end of the first half – Scott Barron headed a dangerous Femi Ilesanmi cross inches over his crossbar and Scott headed the resulting corner narrowly wide.
Brentford started the second half positively but the visitors could have put the game to bed by the hour mark.
Scott headed against the crossbar after beating Bonham to a Gavin Hoyte cross and when the right back delivered again, Reed forced the Brentford goalkeeper in to a save high to his left.
El Alagui stepped on to the Griffin Park pitch for the first time since his serious knee injury with 54 minutes on the clock and had levelled the game by the hour mark.
The visitors could never really clear a corner well won by Dallas on the right – Scott Barron and Hayes combined to put it back in, Saunders’ shot was blocked, the defence managed to deny Dallas but El Alagui was lurking to lash in and complete his own personal comeback.
Brentford’s turnaround took only a few minutes longer.
Dallas latched on to a loose ball in midfield and drove forward, he fed Fillo and the Czech raced away and finished low in to the bottom corner to put Brentford in front.
The game almost exploded when Dagenham captain Abu Ogogo produced a dangerously late challenge on Barron and was lucky to escape with only a caution.
But there was no animosity and the visitors were more concerned with finding a goal.
Brian Woodall shot well wide and then, after Brentford failed to deal with a long throw, turned an effort narrowly off target on the stretch.
Saunders curled a free kick over at the other end and El Alagui narrowly headed a Javi Venta cross wide but the visitors equalised with ten minutes left.
An Ilesanmi cross from the left travelled a long way across Brentford’s area, Woodall headed it back across and Scott slid in to turn the ball home.
The final stages were all action as both teams chased the winner.
Murphy had a shot blocked and Fillo diverted a fierce Ogogo volley away while El Alagui had an effort blocked.
Lewington superbly tipped a Saunders free kick over before Reeves shot wide when he picked up a short corner.
Fillo slid a Saunders cross wide and Woodall missed the target with a free kick before The Bees won it.
Barron delivered a free kick from the left and El Alagui rose highest at the far post to divert a header in to the bottom corner.
There was still time for Hoyte to head powerfully in to the area and cause flutters in the Brentford defence but Mr Miller called time soon after and Brentford were through.
Brentford: Bonham; Venta, O’Connor (sub Craig h/t), Nugent, Barron; Dallas, Reeves, Oyeleke (sub El Alagui 54 mins); Fillo, Hayes, Saunders
Subs (not used): Button, Pierre, Adams, Norris, Clarke
Bookings: El Alagui (58 mins)
Dagenham and Redbridge: Lewington; Hoyte, Doe, Saah, Ilesanmi; Scott, Ogogo, Elito (sub Woodall 72 mins), Howell, Reed (sub Obafemi 68 mins); Murphy
Subs (not used): Seabright, Connors, Bingham, Dennis, Shields
Bookings: Ogogo (67 mins), Doe (88 mins), Howell (90 mins)
Attendance: 3,586 (188 away fans)
By Chris Wickham