Castleford were back on level terms 13 minutes later through Jamie Ellis, who converted his own try for 6-6.
Adam Sidlow added a score of his own eight minutes from the break, with Foster kicking the extras for a six-point lead at half time.
Just two minutes into the second 40 a Kirk Dixon try and an Ellis conversion tied things up once more, however from that point the Bulls took control.
A Matty Blythe try was converted by Foster, who then kicked a penalty to put the hosts 20-12 up.
A further try from Chev Walker and two from Kearney wrapped up the scoring.