Quins skipper Mark McLinden kicked a drop-goal late on in the first half – giving his side a 21-12 half time lead – which eventually proved the difference between the two sides.
The Quins’ try-scorers in that opening forty were Solomon Haumono, Luke Dorn and Lee Hopkins.
Haumono and Hopkins also touched down after the interval to open up a 31-12 lead – but three converted tries from Wigan ensured a nervy finish to the game.
Harlequins boss Brian McDermott admitted his relief that his side had avoided the drop, also paying tribute to the Wigan side.
"It was a massive game, a huge result and gutsy performance – I'm relieved our relegation worries don't have to go down to the last day.
"Solomon Haumono was scary – I'm glad I didn't have to defend against him.
"I thought Wigan played very well especially in the second half and they made it a nervy last ten minutes."
Wigan coach Brian Noble said that Quins played the better and deserved to win; also praising Bryan Fletcher – who scored a hat-trick of tries – and Sean O’Laughlin.
"Harlequins were outstanding in the first half and we didn't really get going. But in the second half we showed how good we can be.
"I thought that Bryan Fletcher had another good game and Sean O'Loughlin was outstanding in the second half.
"But Harlequins had a better kicking game than us and deserved the win."