Whitehaven slipped out of the Coral Challenge Cup, suffering their sixth defeat of the season.
A try eight minutes from the end earned Dewsbury Rams a trip to Newcastle on Sunday in the fifth round.
Before that Whitehaven had enjoyed a long spell of pressure and twice looked to have scored.
Jake Moore lost the ball in a tackle on the Dewsbury line and Brett Carter was ruled offside when he got on the end of a Dion Aiye kick.
It was a break from Aiye on 11 minutes which paved the way for the opening try from Sam Forrester, converted by Moore.
But Dewsbury hit back with a try from a play the ball with Dom Speakman wriggling over. Liam Finn converted.
That sparked a series of Dewsbury attacks and Haven did really well, holding up the opposition three times over their line.
The pressure mounted and on 29 minutes they got in for a second close-range try from Speakman, converted by Finn.
Three minutes before the break Haven were level. They moved the ball well and Andrew Bulman finished in style with a fleet-footed run down the touchline. Conor Holliday converted.
Two minutes into the second half Finn gave Dewsbury the lead again with a penalty.
But on 48 minutes Whitehaven went in front with a well worked try by Jason Mossop.
That 16-14 lead could have been improved as Moore and Carter went close in a crucial spell.
The siege was finally broken and on 65 minutes Finn levelled-up with a penalty from bang in front.
Eight minutes from time Dewsbury scored the winning try when the Whitehaven defence went AWOL and Connor Scott barged over with Finn converting to finish 16-22.Whitehaven: Forrester, Carter, Mossop, Parker, Bulman, Aiye, C. Phillips, Kelly, Newton, Coward, Moore, Holliday, Bradley. Subs: B. Phillips, Dowsett, Walker, Thackray.
Tries: Forrester, Bulman, Mossop
Goals: Moore, Holliday
Dewsbury: Fleming, Brierley, Ryder, Oakes, Dixon, Punchard, Finn, Scott, Speakman, Magrin, Johnson, Thornton, Morris. Subs: Day, Davies, Nelmes, Reilly.
Tries: Speakman (2), Scott
Goals: Finn (5)
Penalties: 5-10
Half time: 12-12
Crowd: 427
Referee: Jack Smith
Star Man: Callum Phillips
Comments: Our rules
We want our comments to be a lively and valuable part of our community - a place where readers can debate and engage with the most important local issues. The ability to comment on our stories is a privilege, not a right, however, and that privilege may be withdrawn if it is abused or misused.
Please report any comments that break our rules.
Read the rules here