Clayton Donaldson fired home the winner three minutes from time after Posh had taken the lead with two second-half goals in as many minutes.
Visiting defender Gabriel Zakuani gave the hosts the lead when he headed Bees right-back Alan McCormack's cross past his own goalkeeper before the break.
But United clawed their way back into it after the interval when Jack Payne equalised with a superb dipping 25-yard volley in the 58th minute.
Brentford were still reeling when Britt Assombalonga capitalised on a half clearance from the home defence to slide the ball beneath keeper David Button to turn the game on its head.
But George Saville glanced McCormack's right-wing cross in-off the post with 10 minutes to go before Donaldson made it five defeats on the trot for Peterborough with the cruel late strike.
Brentford centre-back Tony Craig almost made it four in injury-time but was thwarted by a full-length save from Rob Olejnik.