The Scarlets earned a place in the Amlin Challenge Cup quarter-finals on a dramatic night in the Heineken Cup.
Munster's 51-36 win at Northampton helped the Welsh side's cause.
The visitors were 8-6 ahead at the break thanks to a Rhys Priestland penalty and Mat Gilbert breakaway try to two Rory Kockett penalties.
Aaron Shingler scored the second Scarlets try and Stephen Jones' penalty cancelled out Yannick Forestier's try and Romain Teulet's conversion.
The victory ensured second place in Pool Two behind Munster, but Scarlets' 15 points is not good enough to earn a place in the last eight as one of the two runners-up with most points.
Instead the Welsh region qualify for the second tier Amlin Challenge Cup competition, where they will be seeded seventh and will face a trip to Brive.
The Scarlets were often under pressure at the scrums as they earned a crucial victory in Castres and were stretched almost to breaking point on numerous occasions.
Defensive duties formed the biggest part of the visitors' efforts and they rose to that challenge.
Castres had little to play for in terms of European progress, but with number eight and captain Chris Masoe a leading light, they threatened to overwhelm Nigel Davies' side at times.
But with veteran fly-half Stephen Jones calling the tactical shots for the Scarlets, they managed to keep enough of a foothold in the contest.
And Jones and his team-mates had one significant advantage over their opponents - they were able to take their chances - and with some style.
When full-back Rhys Priestland's kick rebounded off an opponent, the Welsh team also had some good fortune.
The ball fell into the arms of centre Jonathan Davies, who set number eight Mat Gilbert on a 40-metre charge to the line.
It took the intervention of the video referee to approve the score that put them 8-0 up after Priestland's early penalty.
South African scrum-half Rory Kockott kicked two penalties as Castres fought back into contention at 8-6 at the break.
Priestland, Wales' first-choice fly-half heading into the Six Nations, suffered a worrying knee injury that forced him off in the 54th minute.
But by that time, the visitors had stolen further ahead in stunning fashion.
Flanker Shingler gambled on intercepting a pass that would have sent Masoe over unopposed and it paid off.
The flanker then rubbed salt into the wound of the pass Seremaia Bai intended for Masoe by out-pacing the Fijian fly-half in their race to the other end of the field.
Jones was unable to convert and Castres levelled when Yannick Forestier went over amid a ferocious forward drive and Teulet converted.
But after missing a 45-metre penalty, fly-half Jones sent over his final shot from closer range and the visitors held out for a famous win.
Castres: Teulet; Martial, Bonnefond, Garcia, Andreu; Baikeinuku, Kockott; Taumoepeau, Bonello, Wihongi, Murray, Capo, Bornman, Malonga, Masoe (capt).
Replacements: Evans for Garcia (54), Bernard for Andreu (68), Lacrampe for Kockott (70), Mach for Taumoepeau (48), Forestier for Bonello (48), Coetzee for Wihongi (63), Rolland for Murray (68), Caballero for Malonga (61).
Scarlets: Priestland; North, S Williams, J Davies, L Williams; S Jones, R Williams; John, Owens, P Edwards, Reed, Day, Shingler, Pugh, Gilbert.
Replacements: Iongi for Priestland (54), Maule for L Williams (76), R Jones for R Williams (51), I Thomas for John (58), G Davies for P Edwards (51), Timani for Day (65).
Not Used: Phillips, Myhill.
Ref: JP Doyle (RFU).