St Helens substitute Joe Greenwood scored a late try to win the derby at Langtree Park and stop Wigan returning to the top of Super League.
Saints claimed an early lead with two tries on the left touchline from Adam Swift, both improved by Gareth O'Brien.
Wigan then went in front after a Pat Richards try was followed by two from fellow winger Josh Charnley.
But, in a seesaw finale, with just four minutes left, Tommy Makinson's break helped Greenwood win a classic contest.
Wigan were down to 12 men following a yellow card to Australian stand-off Blake Green for showing dissent.
But the Cherry and Whites might easily have been depleted in number earlier in the second half when Scott Taylor appeared lucky to stay on the field for a late shoulder charge on Gareth O'Brien.
While the Saints scrum-half played no further part in the game, Taylor was put on report.
But that incident was the only one really sour note of a stunning, topsy-turvy contest which could have gone either way.
Against a weakened Wigan team without forwards Sean O'Loughlin and Harrison Hansen, as well as star full-back Sam Tomkins, Saints looked in control early on after two tries in the first 17 minutes, both brilliantly converted from the left touchline by O'Brien.
Wigan responded on 26 minutes when Richards got in at the left corner but the winger missed with his conversion attempt, and did so again after Charnley pounced on a knockdown to score a minute from the break.
Wigan trailed 12-8 at the break but led for the first time when they scored seven minutes after the restart.
Having stunned Saints with a brilliant tackle from young Wigan full-back Ryan Hampshire to deny Greenwood, dislodging the ball just as he was about to cross the line, the visitors went straight up the other end of the field to lead for the first time.
Hampshire was again involved, with a one-handed flick to release Charnley, who beat three defenders get in on the right, looping round to get close to the posts - his 98th try for Wigan which Richards converted.
A Wigan transgression allowed Makinson to put Saints level at 14-14 with a penalty, only for Richards to put Wigan back in front with an outrageous drop goal from close to the touchline, with still 20 minutes left.
Another Saints penalty, this time from Jordan Turner, put Saints in front again, 16-15, only for another Wigan drop goal, this time from Saints old boy Matty Smith to level it again.
But a stunning burst down the right from Makinson left Wigan reeling - and Greenwood this time held on as he powered over the line, Turner adding the extras to secure only a fifth home win in 11 league games.
That cements seventh place in the table with five games left, moving Nathan Brown's play-off hopefuls four points clear of ninth-placed Wakefield, while a third defeat in four league games leaves Wigan a point behind Huddersfield, who took over on top on Sunday.
VIEW FROM THE DRESSING ROOM
St Helens coach Nathan Brown:
"I thought our defence was as good as it's been at any stage this year. We hung in there.
"It was by far our best performance to date, It would be nice to finish the year off with more performances like that at home.
"Joe Greenwood had an unhappy start and the early part of the second half wasn't great for him. But he rewarded his team-mates for the faith they had in him.
"The boys showed good character. After we dropped the ball over the line and they scored, we defended our line for a long period and I suppose that was the difference."
Wigan coach Shaun Wane:
"I couldn't afford to risk a few of our players with such a short turn-around.
"That team could and should have won the game. And I thought young Hampshire played really well.
"But we didn't help ourselves by turning so much ball over and giving seven penalties away in the last 20 minutes.
"Saints played out of their skin and we were nowhere near good enough."
MONDAY NIGHT'S DERBY LINE-UPS
St Helens: Lomax; Makinson, Turner, Meli, Swift; Wilkin, O'Brien; Puletua, Howarth, Laffranchi, Manu, Jones, McCarthy-Scarsbrook.
Replacements: Wellens, Clough, Greenwood, Walmsley.
Wigan: Hampshire; Charnley, Goulding, Gelling, Richards; Green, Smith; Mossop, McIlorum, Dudson, Tuson, Farrell, Burke.
Replacements: Flower, Taylor, L Tomkins, Crosby.
Referee: James Child.