Fernando Amorebieta, who joined Boro in March and had only played four times before Friday night, stunned his parent club’s west London rivals when he smashed home a loose ball from inside the box.
The Bees had looked destined for a deserved draw after Andre Gray capitalised on a mistake by visiting goalkeeper Dimi Konstantopoulos and slotted into an empty net on 54 minutes.
Jelle Vossen had given Boro a half-time lead when his unmarked header found the far corner, and now Boro must simply avoid concession in the second-leg at the Riverside next Friday.
Boro striker Patrick Bamford was a surprise start and was off the pace having not trained for a fortnight, but Vossen, who also returned, was a threat for the visitors in the opening half.
Both sides had good spells of early possession but Brentford had the best early chance when Alan Judge fired a wicked shot towards the top near corner that was pushed wide by keeper Konstantopoulos.
Judge, who was lively for the Bees along with Tottenham loanee Alex Pritchard, also went close with a free-kick from 20 yards but his side were already behind through poor defending.
Vossen, scoring his ninth goal of the season, was unmarked in the six-yard box when he nodded Adam Clayton’s cross into the bottom corner on 25 minutes.
The Belgian went close to a second twice before the brake, first forcing David Button to tip round his near post before nudging on George Friend’s header that forced Button to claw the ball back off the line.
Brentford hit back ferociously after the break and Gray, fired up for the second half, gave Boro a warning shot that whistled over the bar before pouncing on a loose touch by Konstantopoulos and slotting the equaliser into an empty net.
Gray went close again, firing a shot over the bar from close range but Bees keeper Button was forced into two good saves to stop Boro retaking the lead, first from Vossen and then from Bamford.
Konstantopoulos made up for his earlier error, producing a superb full-stretch save to stop Jonathan Douglas’ header that was destined for the top corner.
The Bees seemed to have settled for a draw, and were punished when Amorebieta found the net in the dying moments of the game.
Brentford: Button (7) Odubajo (7) Dean (7) Tarkowski (6) Bidwell (7) Douglas (8) Diagouraga (7) Jota (6) Judge (8) Pritchard (7) Gray (8)
Subs used: Smith (6) Dallas (6)
Middlesbrough: Konstantopoulos (6) Whitehead (7) Ayala (8) Gibson (8) Friend (7) Leadbitter (7) Clayton (7) Adomah (6) Vossen (9) Tomlin (6) Bamford (6)
Subs used: Forshaw (6) Amorebieta (7) Kike (6)
Man of the match: Vossen