Wigan Warriors have lost the top spot in Super League after a surprise defeat at struggling Wakefield.
Danny Washbrook and Reece Lyne scored decisive late tries for Wakefield, who have won both games under new head coach James Webster and are 10 points above the relegation zone.
The Wildcats had led 24-18 at half-time, captain Danny Kirmond among their four first-half try scorers.
Liam Farrell scored two of Wigan's five tries but the Warriors drop to third.
Wigan, who went top of the table with a convincing midweek win over Widnes, fell behind when Tim Smith set up Kirmond, but Jack Hughes ran in from 40 metres to reply for a young Warriors side.
Jack Murphy's break provided Farrell with a second Wigan score and Anthony Gelling claimed a high ball to make it 18-6.
However, the Wildcats fought back to take the lead at half-time with three unanswered tries before the interval.
Half-back Smith scored his first since returning to Wakefield on loan from Salford, while Richard Owen and Richard Moore also crossed for the hosts during an excellent spell.
Farrell grabbed his second of the game to make it 24-24 and, although a Paul Sykes penalty put Wakefield back in front, Dan Sarginson showed neat footwork to give Wigan the lead once more.
After Washbrook finished off Paul McShane's pass to restore Wakefield's lead, Lyne collected an offload from Sykes to secure a vital victory.
Meanwhile, Wigan coach Shaun Wane has confirmed hooker Michael McIlorum is likely to be out for about eight weeks with a broken arm.
Wakefield Trinity Wildcats head coach James Webster: "It was an exciting game. I thought the boys played really tough and gutsed it out.
"Wigan came at us for the full 80 minutes. I thought it was a decent game of rugby if you're a neutral, and you will have enjoyed it.
"But as a Wigan or a Wakefield supporter there were a couple of moments where it was heart-in-your-mouth sort of stuff.
"I'm under no illusions that we got them at a good time. The first day I spoke to my staff preparing for Wigan, I said we had a massive advantage."
Wigan coach Shaun Wane: "Too many penalties cost us good field position. We lacked a bit of energy. I think three games in a week got to them a bit, a young team.
"You can't come to places like this in Super League and give penalties away like that.
"The best team won. Wakefield seemed to want it a bit more than us today.
"We played poorly today but we still could have sneaked it with a bit more composure and that's what your leaders give you. We lacked them today."
Wakefield: Mathers; Owen, Ryan, Lyne, Riley; Sykes, T Smith; Anderson, McShane, Scruton, Gilmour, Kirmond, Washbrook.
Replacements: Godinet, Moore, Tautai, D Smith.
Wigan: Sarginson; Charnley, Goulding, Gelling, Murphy; S Powell, Smith; Taylor, Williams, Flower, Hughes, L Farrell, Burke.
Replacements: James, C Farrell, Sutton, Wright.
Referee: George Stokes