Iestyn Harris kicked a last-gasp penalty as Bradford snatched a dramatic win at Castleford to avoid a tough Super League play-off trip to France.
The Bulls had struggled to shake off the league's bottom side despite a hat-trick from winger Semi Tadulala.
With Warrington losing to Huddersfield, the late win was enough to take the Bulls above the Wolves into fifth.
That means they will now play Wigan and avoid a tricky play-off trip to Catalans Dragons.
Tadulala put the Bulls ahead with a converted try but scores from Richard Owen and Kirk Dixon plus a Dixon penalty put Cas in front.
Tadulala's try levelled the scores at 10-10 at the break but Cas went back in front just after the hour as Ryan Boyle twisted and turned his way over under posts for another Dixon-converted try.
Bradford dug deep and Tadulala, who looked dangerous on every carry, cut inside to score with 10 minutes remaining.
Harris, who came off the bench, nailed the difficult conversion to level the scores at 16-16 and then created the opportunity to win the game.
He beat his man with an outrageous dummy and although Ned Catic caught him he was penalised and sent off for holding down.
That gave Harris a shot at goal and he held his nerve to secure the points.
Bradford coach Steve McNamara:
"We did some good things and some not-so-good things, so we have won in a different way.
"It was an ugly sort of win but it was important we knocked three wins off on the bounce.
"I thought we played well within ourselves tonight, with an eye on next week."
Tigers coach Terry Matterson:
"I don't think we deserved that but I couldn't have been more proud of the boys.
"It was a fair penalty but what we want is consistency. We should have had a penalty about five minutes before that.
"We had our opportunities but it didn't go our way. I thought it was very courageous from both sides and I thought a draw would probably have been fair."
Castleford: Donlan, Owen, Shenton, Dixon, Wainwright, McGoldrick, Dorn, Sargent, Sargent, Henderson, Korkidas, Korkidas, Guttenbeil, Catic, Lupton, Leafa, Higgins, Huby, Boyle.
Replacements: Higgins for Sargent (16), Guttenbeil for Sargent (68), Huby for Henderson (55), Huby for Korkidas (11), Higgins for Korkidas (68), Sargent for Guttenbeil (53), Henderson for Catic (61), Catic for Lupton (72), Boyle for Leafa (48), Korkidas for Higgins (52), Leafa for Huby (34), Leafa for Boyle (75).
Sent Off: Catic (80)
Bradford: Sykes, Halley, Evans, Nero, Tadulala, Jeffries, Deacon, Vagana, Vagana, Vagana, Newton, Lynch, Cook, Finnigan, Langley, Godwin, Kopczak.
Replacements: Harris for Deacon (51), Solomona for Vagana (15), Lynch for Vagana (51), Kopczak for Vagana (75), Godwin for Newton (29), Vagana for Lynch (31),
Langley for Finnigan (59), Kopczak for Langley (27), Newton for Godwin (49), Vagana for Kopczak (64).
Ref: I Smith (Oldham)