The Ospreys overturned a half-time deficit with a try from the inspirational Alun Wyn Jones, a penalty try and three place-kicks from Dan Biggar and James Hook.
But O'Gara denied them at the death, drilling over a close range penalty after Ospreys skipper Jones infringed at a ruck in front of the posts.
The defeat is hugely significant for the Ospreys as they have fallen to fifth place in the league table and their hopes of reaching the play-offs now hang by a thread.
The defending champions are a point behind the fourth-placed Cardiff Blues and even if they muster a bonus point win in the final round against Aironi, it might not be enough as the Blues have two more games to play against the Dragons and the Scarlets.
As things currently stand, Munster have a home semi-final, Leinster are destined to finish in the top four and Ulster, in third, are poised to join them. They are four points ahead of the Ospreys.
The Swansea-based side's fourth meeting of the season with Munster was scoreless until the 24th minute, The best of the early scoring chances fell to the visitors.
Munster used lineout possession to invite Keith Earls through on the left and he just knocked on as he tried to gather the ball over the try-line, with Richad Fussell putting him off.
Then an encouraging attack from the Ospreys saw hooker Richard Hibbard put Hook over in the left corner but the try was ruled out, following the intervention of television match official Tony Rowlands, for a foot in touch. Credit to Conor Murray for an excellent covering tackle.
The deadlock was soon broken by O'Gara who converted a penalty from the 22, and his neat chip almost created a try for the onrushing Lifeimi Mafi.
Ryan Jones had to go off for a blood injury and there was further disruption for the Ospreys as the Clermont Auvergne-bound Lee Byrne limped off in his final home appearance for the region.
The men in red managed to get over the line in the 33rd minute through flanker Wallace. He bulldozed his way over on a powerful burst to the line, with O'Gara adding the conversion.
A penalty from Biggar closed the gap to 10-3 for half-time, but the Ospreys handed those points back early in the second half when O'Gara kicked another penalty.
Biggar was just wide of the mark with a penalty attempt four minutes later, but the Ospreys got right back in the contest by scoring two converted tries in the space of seven minutes.
With the regional outfit raising their game, Jones got over the whitewash from a well-orchestrated maul drive which cut Munster's lead to 13-10.
The Ospreys were beginning to click in attack and winger Fussell went on an eye-catching run which put the hosts in a a good position to score again.
They turned the screw up front, pressurising the Munster front row in a series of five-metre scrums from which they earned a penalty try to go 17-13 ahead.
However, the Ospreys could not break clear of Munster on the scoreboard. The reliable boot of O'Gara reeled the home side back in as he fired over successive penalties after 69 and 72 minutes.
Munster's lead was shortlived though, with centre Hook, in his last home game before his move to Perpignan, coolly slotting a 76th minute penalty after Biggar was replaced.
Time was not on Munster's side and with nothing to play for ahead of next weekend's Amlin Challenge Cup semi-final against Harlequins, they could have opted to settle for a losing bonus point.
But O'Gara and replacement scrum half Peter Stringer kept Munster working hard and the former watched a drop goal attempt miss the target.
The Corkman made amends by putting over the decisive penalty after the Ospreys, who had former All Black Jerry Collins sin-binned for a high tackle on Felix Jones, were caught offside.
It was a fourth consecutive defeat for the Ospreys and a devastating blow at the end of a week in which long-serving winger Shane Williams signed a new two-year deal with the region.
|Ospreys Score Card|
|Munster Rugby Score Card|