After starting the season with a fantastic 19-game unbeaten run this result ended a disappointing week for the Stags after being knocked out of the Carabao Cup by Port Vale on Tuesday night.
The game was played on a sodden surface in continual rain with heavy patches in parts where the ball held up and made life tough for both sides.
The two teams had fought out a tight, exciting 0-0 draw in League Two action at the One Call a month ago.
That one saw Stags dominate.
But this one was much more open with end to end stuff until Wrexham went ahead on 23 minutes through Sam Dalby.
Then it took the crossbar to deny Dalby a second before Christy Pym had to make a brilliant double save to deny Dalby and Elliot Lee to keep it 1-0 at half-time.
Stags came out on the front foot after the break, creating chances only to see Paul Mullin make it 2-0 on 58 minutes.
But Oates grabbed a superb solo effort two minutes later to keep the game alive with further chances at both ends to keep both sets of fans on the edge of their seats to the very end.
The Stags side showed three changes with a long-awaited start after injury for striker Oates.
Defender Cargill and Johnson both came in to play the full back roles. Macdonald, Maris and Swan dropped down to the bench with Bowery pushed up front from right back.
Boateng was initially on the bench but felt ill before kick-off and had to be replaced by Abdullah at the last minute.
A damp and misty evening began with the traditional Remembrance Day ceremony on the pitch pre-match.
There was a lively start as Stags went straight on the front foot only for Wrexham to counter and Cannon to zip a low shot just wide from 20 yards as we entered the second minute.
A minute later Keillor-Dunn threaded Oates in one on one, but Okonkwo was able to block at his near post.
In Stags' next raid Oates had a shot blocked and Keillor-Dunn made space with the loose ball to pull a low shot just wide.
On 14 minutes Keillor-Dunn warmed Okonkwo's hands from 25 yards, but the shot was always going straight to the keeper.
In reply Dalby saw a header deflect wide from a free kick.
The best move of the game so far came on 17 minutes as Johnson put Oates into space on the right and from the y-line he lashed the ball across goal where Bowery was inches away from turning it home.
A minute later came Wrexham's best threat to date as Dalby jinked his way past Reed in the box before forcing Pym to save with his legs.
Clarke then had a shot deflect over as an enthralling game swept from end to end.
But the visitors were ahead on 23 minutes after a cutting move down the centre which ended with McClean finding Dalby in the box and he crashed an unstoppable finish high past Pym.
Great skill from defender Cargill saw him beat three opponents before squaring for Reed who flashed a low, first time finish wide.
Oates was next to have a shy at the Wrexham goal, firing too high after doing well to make the space 20 yards out, and Lewis was also wide soon after as Stags probed for a way back.
On the half-hour a superb, disguised reverse pass by Lee gave Dalby another sight of goal from a similar position from where he had score,d but this time his finish cannoned back off the bar.
Cargill saw a first time shot from a low Bowery cross deflect wide for a fourth home corner.
Evans was booked on 32 minutes for hauling down Flint.
Stags were dealt a blow on 40 minutes as Clarke needed treatment and had to come off to be replaced by Maris.
A poor pass from Reed put Stags under pressure on 42 minutes and Pym had to produced a superb double save to deny Lee and then Dalby on the follow-up.
A minute later Mullin played a one-two in the box with Dalby but Pym was able to push away his finish for a corner as Wrexham finished the half strongly.
Three minutes into the second half Reed forced Okonkwo to save his rising finish from Johnson's pass as Stags signalled their intent.
A minute later Keillor-Dunn sent Oates into the box, but the keeper was again there, making himself big to block.
Maris had Wrexham sweating when his cross almost dropped under the far angle and on 53 minutes Bowery came desperately close as he met a Keillor-Dunn cross with a near post volley that passed the wrong side of the upright.
Soon after Oates drilled a low ball across the box from the left and Keillor-Dunn completely missed his kick from close range with what would have surely been the equaliser.
Keillor-Dunn curled a free kick into the sidenetting as Mansfield continued to press hard.
It was all Stags so it was a cruel blow when Wrexham doubled their lead on 58 minutes.
Johnson's clearance hit an opponent and rolled nicely for Dalby to set up Mullin for a well-placed finish on
But a minute later Oates won the ball off an opponent and buried a thunderous finish to re-ignite home hopes.
Seconds later Bowery skied a close range finish with the home crowd now in full voice.
Cannon then whipped a low shot inches wide first time for Wrexham.
McLaughlin and Swan came on for Johnson and Cargill on 64 minutes to give fresh legs on the sapping surface.
Reed was late in on Cannon on 68 minutes to earn a caution, the Wrexham man needing lengthy treatment and eventually limping off.
Mullin made Pym get down to save on 81 minutes as the Welsh visitors continued to look dangerous on the counter.
Gale and Macdonald were pitched into the action with six minutes left.
In the final minutes of the 90 Maris was just wide on the turn from 15 yards.
Eight added minutes kept home hopes faintly alive and three minutes into it Davies was booked for bringing down Keillor-Dunn.
But there was to be no fairytale ending as the Welsh side progressed into round two.
STAGS: Pym, Johnson (Swan 64), Flint, Brunt, Cargill (McLaughlin 64), Reed, Lewis (Macdonald 84), Clarke (Maris 40), Keillor-Dunn, Oates (Gale 84), Bowery. SUBS NOT USED: Flinders, Cooper, Flanagan, Abdullah.
WREXHAM: Okonkwo, Evans, Tozer, O'Connor, Forde (Mendy 66), Jones, Cannon (Young 70), Lee (J. Davies 82), McClean, Dalby (Palmer 82), Mullin. SUBS NOT USED: Howard, Cleworth, Boyle, J. Davies, Bickerstaff, Hosannah.
REFEREE: Ed Duckworth.
ATTENDANCE: 6,158 (560 away).