Ronan O'Gara scored a winning drop goal in overtime for the second week in a row to clinch victory for Munster.
The Irish province had trailed for most of the game but battled back with tries by Doug Howlett, Peter O'Mahony and replacement winger Will Chambers.
Castres started strongly and grabbed first-half tries by hooker Brice Mach and winger Pierre-Gilles Lakafia.
The match was tied at 24-24, with the clock in red numbers, when the Munster pack set up O'Gara's winning kick.
It leaves Munster second in Pool One, the Scarlets leading by one thanks to their bonus point against Northampton on Friday night.
Castres, playing on the ground of their great rivals Toulouse, may feel unlucky to have taken just a bonus point from the closely-fought encounter.
However, Munster's experience proved decisive yet again as the twotime winners secured a valuable victory in the south of France.
It was Castres who started brightest, with Bernard's long-range penalty from the left putting them 3-0 ahead in the third minute.
The fly-half then quickly landed another wind-assisted penalty from inside his own half.
The French side got their first try in the 13th minute when captain Chris Masoe burst his way into the Munster 22. The ball was quickly moved from left to right, for Mach - on for the injured Marc Andreu - to get over in the right corner for 11-0.
Munster's forwards gradually started to make an impression and they were finally rewarded after 23 minutes.
When the strong-running Wian du Preez was held up in front of the posts, some good handling by scrum-half Conor Murray and number eight James Coughlan allowed 33-year-old former All Black Howlett to score on the right.
The Heineken Cup's record points scorer O'Gara nailed the conversion from the right corner of the 22.
But Castres scored another try three minutes before half-time after Paul O'Connell surrendered possession deep in hos own half.
Castres took full advantage, sweeping the ball swiftly across the 22 and Lakafia stepped inside the exposed Howlett to touch down in the right corner.
Bernard added the extras and Castres led 18-7, before O'Gara scored a penalty with the last kick of the half after a succession of five-metre scrums.
The Irish side drew to within one point five minutes into the second half when O'Mahony burst through a huge gap in the home defence for a try which O'Gara converted.
Bernard responded with a penalty to stretch Castres' advantage to 21-17 but the hosts handed Munster a try just moments later.
As replacement Remi Tales tried to clear on his own line, O'Gara charged his kick down and Denis Leamy fed fellow replacement Chambers for an easy score.
O'Gara converted as Munster found themselves in front for the first time but Bernard levelled things with 10 minutes left, setting up a thrilling climax.
Munster battered Castres for the final few minutes, and Tomas O'Leary's pinpoint pass gave O'Gara just enough time to rifle over the match-winning drop goal from the 10-metre line.
Castres: Denos, Evans, Bonnefond, Garcia, Andreu, Bernard, Lacrampe, Peikrishvili, Mach, Wihongi, Murray, Tekori, Bornman, Caballero, Masoe.
Replacements: Tales for Denos (63), Lakafia for Andreu (5), Teulet for Lacrampe (70), Coetzee for Peikrishvili (56), Bonello for Mach (12), Ducalcon for Wihongi (46), Rolland for Murray (73), Capo for Caballero (61).
Munster: Murphy, Howlett, Barnes, Mafi, D Hurley, O'Gara, Murray, du Preez, Varley, Botha, D. Ryan, O'Connell, O'Mahony, Ronan, Coughlan.
Replacements: Chambers for Barnes (43), O'Leary for Murray (56), D. O'Callaghan for D Ryan (50), Fogarty for O'Mahony (70), Leamy for Coughlan (66).
Not used: Horan, J Hayes, Keatley.
Referee: Wayne Barnes (England)