The visitors were quickest out of the starting blocks as two tries from Adam Swift and one from Mark Percival, combined with conversions from Luke Walsh, put their side 18 points up after just a quarter of an hour played.
Castleford recovered quickly from their early setback and were back level by half time, thanks to tries from Luke Dorn and Ben Roberts supplemented by Luke Gale conversions, and five minutes after the restart Dorn edged the hosts into the lead.
Percival crossed over for Saints in the 53rd minute to level the scores again, but Gale's penalty goal edged Castleford in front once more.
Denny Solomona crossed over in the 66th minute to keep Castleford hot on their opponents' heels, but a third try of the game from Swift allowed St Helens to extend their 10-point lead.
Junior Moors gave the hosts a glimmer of hope when he got over with just two minutes left, but the New Zealander was called out for a forward pass moments after Gale's conversion, allowing St Helens to see out the game.
The result sees Saints climb to third in the Super League table, while Castleford remain fifth and are left with little to play for in the remainder of the season.