Late tries from Micky Higham and Chris Hill helped Warrington to end Wigan's run of 11 straight Super League wins.
Ben Westwood and Joel Monaghan put the hosts 10-0 up at the break, but the Warriors responded with scores from Sam Tomkins and Anthony Gelling to move two points ahead.
However, Higham's opportunist effort and Hill's score under the posts earned a famous victory for Warrington.
Wigan remain three points clear at the top of Super League.
Shaun Wane's Wigan side would have moved a huge step closer to retaining their League Leaders' Shield with a victory at the Halliwell Jones Stadium, which looked likely when they led 12-10 heading into the final five minutes.
Instead, they must reflect on a first defeat since mid-March, while second-placed Huddersfield and Warrington - who have closed the gap between themselves and the summit to just four points - will both feel they can overhaul the Warriors with eight matches of the regular season still to go.
Both sets of players left the field to rapturous applause from both sets of supporters, a testament to their efforts in a bruising and enthralling encounter, and on this evidence it would not be a surprise to see the two sides contesting the Grand Final at Old Trafford in October.
After seeing off three consecutive Wigan sets in the opening three minutes, Warrington opened the scoring with their first meaningful attack of the game.
Veteran stand-off Lee Briers, whose late drop-goal earned the Wire a 17-17 draw at the DW Stadium in February, was the architect as his short pass sent England second-rower Westwood over from close range.
Moments after full-back Brett Hodgson had fallen agonisingly short of a second Warrington score, winger Monaghan eventually extended their advantage on the half-hour with his 19th Super League try of the campaign, although it took more than two minutes for the video referee to confirm he had applied downward pressure to a grubber kick from Briers.
That the home side prevented a depleted Wigan, missing nine first-teamers through injury, from registering any points in the first period was as a result of some heroic last-ditch tackling.
Warriors captain Tomkins and later winger Pat Richards both seemed certain to touch down, but were both held up in goal by several in primrose and blue, as a breakthrough eluded Wane's league leaders before the interval.
As has so often been the case in recent seasons, it was Wigan's talisman Tomkins who provided the platform for their comeback, shaking off the attentions of Hodgson and stretching out a right arm to ground the ball over the whitewash within three minutes of the restart.
Their defence finally breached, Warrington appeared rattled and after another sustained spell of pressure it came as no surprise when Cook Islands centre Gelling collected a pass from Tomkins to cruise over unopposed in the corner.
Having survived a stunning set of six perpetrated by Wolves scrum-half Richie Myler's break from deep inside his own half, Wigan looked like holding on.
However, Chris Riley broke free down the left wing and kicked ahead for hooker Higham, who dived on the ball in front of a jubilant home crowd, before prop Hill's try in the dying seconds sealed a crucial win.
VIEWS FROM THE DRESSING ROOM
Warrington Wolves head coach Tony Smith:
"Wigan had enough ball to win four games. The amount of defence we went through was enormous.
"To keep them to the points we did, with the amount of ball they had on our line, was an enormous effort.
"I thought it was one of those games that either team could have won.
"It was a good game of rugby league; end-to-end, entertaining, tough, very physical and a great advert for our sport. It's the sort of standard that rugby league should be played at."
Wigan Warriors head coach Shaun Wane:
"I don't want to take anything away from Warrington. They're a quality team and probably the best team won on the day, but they know they've been in a game.
"I'm very proud of our effort, when you consider we've got nine out [injured].
"To come to a place like this, against a team like Warrington - who are probably the form team in the competition at the moment and who score as many points as they do - to do what we did was a great effort from our players and I'm really proud of them."
Warrington Wolves: Hodgson; Riley, Atkins, Ratchford, J. Monaghan; Briers, Myler; Wood, Higham, Hill, Waterhouse, Westwood, Grix.
Replacements: Morley, M. Monaghan, Carvell, Currie.
Wigan Warriors: S. Tomkins; Charnley, Goulding, Gelling, Richards; Powell, Smith; Mossop, L. Tomkins, Crosby, Hansen, Farrell, Burke.
Replacements: Hughes, Tuson, Spencer, Williams.
Referee: Phil Bentham (Warrington)