The visitors were three-up at half-time after two clinical strikes from inside the penalty area in the first six minutes from Darren Potter and Angelo Balanta had given them an early lead.
Former Bee Charlie MacDonald then fired home the Dons’ third just before the break to warm applause from the Griffin Park crowd.
But Uwe Rosler’s Brentford hit back just after the hour-mark, Gary Alexander hammering home a loose ball in the area following Sam Saunders’ corner.
Alexander grabbed his second in the 75th minute, this time making no mistake from the penalty spot after Adam Smith was adjudged to have handled.
Marcus Bean flashed a header over the crossbar from close range minutes later as Brentford piled on the pressure and the comeback was complete in the final minute when Legge powered Saunders’ free-kick against the crossbar and followed up to force home the rebound.