Chris Tuson's late try moved Wigan up to second in Super League with a dramatic victory over Castleford.
Tries from Ben Flower, Pat Richards and Tuson gave Wigan a 16-point lead, but Jake Webster's score closed the gap.
Michael McIlorum stretched Wigan's advantage again, before two tries from Justin Carney and one from Jamie Ellis brought the hosts level at 22-22.
Tuson added his second late on to win it for Wigan, while Richards took his tally with the boot to eight points.
Wigan went into the game with an impressive seven wins in their last eight meetings with Castleford - the other being a 22-22 draw in June 2011.
Remarkably, they let slip a 22-4 lead on that occasion but Wigan did not have to settle for a draw this time and held their nerve to go on and win the game.
The visitors stormed into a commanding lead as Blake Green's short pass sent Flower over, Richards rounded off a flowing cross-field move and Tuson scored with a close-range finish.
Carney set up Webster to close the gap to 16-4 at half-time but, any hopes of a Castleford comeback seemed to disappear when McIlorum was allowed to worm his way over from dummy half.
But the hosts' fightback really began when Ellis went in under the posts following excellent work from Webster, while Carney scored two tries in eight minutes to level the scores.
Rangi Chase struck the post for Castleford with a late drop-goal attempt, before Tulson took Sean O'Loughlin's excellent offload to score his second and win the game in the process.
It was a first home defeat for the Tigers, who sit one point above basement club Salford.
VIEWS FROM THE DRESSING ROOM
Castleford boss Ian Millward said:
"It was a tough way to lose right at the death there, but I can't fault our players' commitment or attitude. There wasn't much in the game.
"I'm really happy with the progress we've made. The effort was very strong, but you like with effort to get the result too.
"At 22-all it's obviously a great finish; it would have been nice to finish it off and back up that effort. I thought the players were entitled to something."
Wigan coach Shaun Wane said:
"I'm happy to get the two points but the manner we got them in I'm a bit disappointed with.
"I thought we played well in parts, but we just weren't consistent enough.
"I'm glad we showed some steel in finishing it off, but that was definitely a get-out-of-jail card and you've got to commend Castleford for coming back."
Castleford: Tansey, Carney, Shenton, Webster, Dixon, Chase, Ellis, Emmitt, Clark, Mason, Gilmour, Hauraki, Millington.
Replacements: Huby, Massey, Holmes, Thompson.
Wigan: S. Tomkins, Charnley, Hughes, Thornley, Richards, Green, Smith, Dudson, McIlorum, Taylor, Tuson, Farrell, O'Loughlin.
Replacements: Crosby, Flower, L. Tomkins, Lloyd.
Referee: Thierry Alibert (France).