This one looked to be heading for a draw after Pierre Bernard drew the sides level with a penalty but O'Gara instead earned his side a win with a superbly struck drop goal in added time.
Clearly the keener side in the opening quarter, Castres led 11-0 thanks to two Pierre Bernard penalties - the second from inside his own half - and try from Brice Mach in the 11th minute, the hooker replaced immediately after.
Munster finally got on the scorboard with 22nd minute Doug Howlett try that O'Gara converted superby to compensate somewhat from an earlier missed drop-goal but he missed an opportunity to reduce the deficit shortly after from a penalty into a stiff breeze.
However, Castres extended their lead when Pierre Lakafia, a 2nd half replacement for Mark Andreu, went around Howlett to score his side's second try, Bernard adding the points.
Afer a succession of five metre scrums, O'Gara kicked an injury time penalty to send Munster in trailing 18-10.
Peter O'Mahony opened Munster account early in the second half, O'Gara converting his fine effort with an equally fine conversion before Bernard pushed the gap out to four points with a 53rd minute penalty.
Then replacement Will Chambers scampered over for a try that O'Gara converted before producing yet again that piece of magic that makes such a special player.
Munster: J Murphy; D Howlett, D Barnes, L Mafi, D Hurley; R O'Gara, C Murray; W du Preez, D Varley, B Botha, D Ryan, P O'Connell [capt], P O'Mahony, N Ronan, J Coughlan. Replacements: D Fogarty, M Horan, J Hayes, D O'Callaghan, D Leamy, T O'Leary, I Keatley, W Chambers
Castres Olympique: F Denos; M Evans, P Bonnefond, P Garcia, M Andreu; P Bernard, T Lacrampe; A Peikrishvili, B Mach, K Wihongi, S Murray, I Tekori, J Bornman, Y Caballero, C Masoe [capt] Replacements: M Bonello, M Coetzee, L Ducalcon, M Rolland, R Capo Ortega, R Teulet, R Tales, P Lakafia
Referee: Wayne Barnes (England)