In a close game, the visitors took the lead in controversial circumstances against the run of play in the ninth minute, when referee Andy Hall awarded a free-kick after stopping play to book Nathan Stanton for dissent, allowing Sam Wood the chance to curl in a cross that was nodded home by Jordan Rhodes.
Brentford again netted against the run of play again five minutes later when Wood beat Nicky Adams and Scott Wiseman before squaring for Charlie MacDonald who side-footed home.
The hosts were awarded a penalty in the 34th minute when Ryan Dickson was penalised for handling in the area, Tom Kennedy pulling a goal back from the spot.
MacDonald should have restored the two-goal advantage when he rounded Frank Fielding but slid a shot wide of the open goal.
After the break, Rhodes wasted a great chance to wrap up the result but flicked harmlessly at Fielding while Adam Le Fondre almost capitalised when Ben Hamer spilled a late cross.