Bees boss Uwe Rosler made 10 changes from the side that drew 1-1 at Port Vale on Saturday and the hosts fell behind following a Ben Nugent own goal.
Second-half goals from substitute El Alagui and Martin Fillo put Brentford in control but Dagenham hit back through Josh Scott before El Alagui's late header eradicated the need for extra time.
Things started poorly for the League One side as defender Nugent marked his debut in acrimonious circumstances in the 15th minute.
Josh Scott's scuffed shot was turned past Jack Bonham by the outstretched Nugent, although referee Pat Miller took a moment to award the goal, consulting his assistant who had initially flagged Rhys Murphy offside.
Scott then came close to doubling the visitors' lead early in the second half when he guided a header on to the crossbar with Bonham off his line and nowhere near the cross.
El Alagui was introduced but was soon shown a yellow card for diving early in the second half, although he soon redeemed himself as he was on hand to steer the hosts level on the hour-mark after a scramble in the penalty area.
The League One side were soon ahead as Fillo, making his full debut after his loan move from Viktoria Plzen, struck a low shot past Lewington before Dagenham levelled with just over 10 minutes to go as Scott converted from close range.
But Bees striker El Alagui headed home a deep free-kick four minutes into injury time to put his side into the draw for Round 2.