A last-minute try from Jake Webster gave Castleford Tigers victory over Salford Red Devils.
The hosts had the better of a tight first-half with Justin Carney's try, converted by Marc Sneyd, giving them a 6-0 lead at the break.
After a Sneyd penalty increased their lead Junior Sa'u and Gareth Hock crashed over, with Rangi Chase converting to put Salford in front.
It looked like that would be enough but Webster went over at the death.
The Tigers went into the game off the back of successive defeats and both teams struggled in the wet conditions early on.
Carney gave them a much-needed lead with their first meaningful attack of the game when he latched on to Liam Finn's kick.
Snyed's conversion left the scores at 6-0 at the break and it was the on-loan Salford man who scored the first points of the second half when he kicked a penalty.
Salford were handed a lifeline when Carney spilled the ball in the tackle and Sa'u forced his way over the line.
The visitors then went in front when Hock barged over and former Castleford man Chase landed the conversion.
Webster won it for the hosts when he took Daryl Clark's pass and went over in the last seconds to break Salford hearts.
Castleford move back up to fourth while Salford sit in 12th but are nine points clear of relegation danger.
Castleford coach Daryl Powell: "Obviously when you account for the fact we've scored on the last play of the game we feel like we've got out of jail a little bit.
"I thought we were in control at 8-0, but it gets like that when you've lost a couple. We came up with a couple of smart plays to get the job done eventually, but on the balance of possession you'd have expected them to win the game.
"However, it's about winning and I thought we did enough to come out on top at the end. The conditions were tough and you need that desperation to win games - and I thought we had that in abundance."
Salford coach Iestyn Harris: "It's very frustrating, and very tough to take. I thought we were the better side, we created the better chances and I was frustrated that we had clean breaks that were slowed down.
"I'm surprised you don't get a yellow card for one of those. I don't think second-half it had too much effect on proceedings, it was greasy in the first-half but we've been gradually getting better defensively for a period of weeks.
"I don't think Castleford could break us down until that final play, but I don't know where all that extra time came from."
Castleford: Dorn, Dixon, Webster, Shenton, Carney, Sneyd, Finn, Lynch, Clark, Huby, Millington, Hauraki, Wheeldon.
Replacements: Mariano for Lynch (58), Milner for Clark (53), Jewitt for Huby (51), Carvell for Wheeldon (31).
Salford: Evalds, J. Griffin, Meli, Sa'u, Johnson, Chase, Fages, Morley, Lee, Tasi, Ashurst, Hock, Puletua.
Replacements: Tomkins for Morley (33), Rapira for Tasi (27), Walton for Hock (11), J. Walne for Puletua (24).
Ref: Tim Roby (RFL)