Paul Sykes's late try saw Wakefield win at Widnes and took them above Salford into 11th in the Super League table.
The Wildcats raced ahead thanks to Chris Riley and Richard Moore scores.
But Declan Hulme and Paddy Flynn crossed to tie things at 12-12, before Hulme grabbed his second straight after the break to put the hosts in front.
Harry Siejka levelled the scores again, before the visitors snatched it in the final minute as Sykes stole in with the Vikings defence expecting a drop goal.
It secured back-to-back wins after Wakefield's victory over Hull FC last time out, an impressive pair of results to halt a six-match losing sequence.
The victory was a result built on strong defence in the second half, with Sykes's score coming in a rare venture forward.
That was a stark contrast to the game's opening quarter as Jarrod Sammut ran the length of the field and unselfishly offloaded for Riley to score, before Moore barged his way over to put Richard Agar's men firmly in control.
But Kevin Brown, whose spill allowed Sammut to lay on the opening try, then threw two perfect cut-out passes - first for Hulme and then for Flynn to set up a thrilling second period.
Hulme touched down again soon after the restart, leaving Danny Tickle to add the extras as the hosts went ahead for the first time.
Siejka's converted effort tied the scores at 18 each and his side were forced into desperate defence from then onwards, but they stood firm.
And it was Sykes who had an age to pick his spot with a drop goal just outside the 20, but the Wakefield man's left-footed effort drifted wide.
But, after Craven and Tickle then missed drop-goal attempts either side of the latter pushing a difficult goal attempt wide, Sykes made amends.
Widnes were expecting Sammut to drop for the one-pointer but, with the Vikings defence rushing out, he fed Sykes who burst through to snatch victory.
After the final whistle there was concern from both sets of players as Brown appeared to collapse after taking a heavy challenge late in the game.
Wakefield coach Richard Agar:
"We weren't too impressive with the ball but defended well.
"It was a scruffy game and both sides had plenty of opportunities to win it, but we were incredibly tough and determined.
"Although, had it gone the Vikings' way, we couldn't have had too many complaints.
"We had only one interchange left at the end and were under the pump, but I was pleased with our toughness, resilience and determination."
Widnes coach Denis Betts:
"We should have won and a draw may have been a fair result, but it was messy and sloppy and we kept giving them chances.
"We've got to take these lessons on board. Our concentration levels slipped and we switched off.
"But our full-back Jack Owens was the best player on the field and Declan Hulme also played well - we had enough opportunities to win the game.
"Kevin Brown is being treated for concussion. He seemed to get hit a bit late with a shoulder charge."
Widnes Vikings: Owens, Flynn, Dean, Marsh, Hulme, Brown, Craven, Johnson, Clarke, Gerrard, Allen, Tickle, Leuluai.
Replacements: Isa, Kavanagh, White, Clough.
Wakefield Trinity Wildcats: Sammut, Mathers, Ryan, Walshaw, Riley, Sykes, Siejka, Anderson, McShane, Scruton, Washbrook, Kirmond, Smith.
Replacements: Godinet, Moore, Molloy, Fairbank.
Referee: Ben Thaler.