Argentinian wing Martin Moyano kicked all of the pre-season relegation candidates points but they nearly lost the match at the end only for Romain Teulet to miss a late kickable penalty.
"I am virtually speechless," said Montpellier's joint coach Fabien Galthie.
"Yes this is great. We never gave up, we just stuck to them snapping at their heels.
"It was an unbelievable match. At times we looked to be finished but we just never let up. And what is more we have prevailed away from home."
Castres, who finished third in the league table were the huge favourites going into the match and got off to the perfect start when Ibrahim Diarra scored a converted try after only four minutes.
Monyano responded with three penalties but a penalty try that again was converted just before the break gave Castres a 14-9 half-time lead.
However, Montpellier had three penalties in the second-half compared to one from Castres to complete a famous win.