New Notts County manager Ian McParland kicked off with a win from his first match in charge at Meadow Lane but it was not until stoppage time that victory was ensured.
Notts left it late after dominating the first 45 minutes, then allowing Wrexham to come back strongly at them in the second period.
It looked like heading for a 1-1 draw when Hector Sam, off the substitutes' bench for only a few moments, was hauled down a yard outside the penalty area by Silvio Spann.
Spann was red carded while Notts got a free-kick in a central position which midfielder Neil MacKenzie curled spectacularly inside goalkeeper Anthony Wiliiams' left-hand post for his fourth goal of the season.
Notts had started the game easily the better of the two sides yet it was an absolute gift goal which saw them go ahead on 17 minutes.
Steve Evans made a careless clearance which exposed his goalkeeper and left Spencer Weir-Daley with a simple chance for his first goal at Meadow Lane.
Notts nearly doubled their lead on 24 minutes with a brilliant drive by Myles Weston which Williams got his fingertips to and turned around his far upright.
Next it was the turn of the Notts goalkeeper, Kevin Pilkington, to be in the action when he turned a good effort by Richard Hope over his bar.
Wrexham improved in the second half and Pilkington had to make another good save on 63 minutes before Notts stormed back for Weston to feed Matt Somner who fired over the top.
The Welshmen, desperate for points, were not to be denied and levelled with 20 minutes to go with a spectacular low drive into the corner by Spann.
Then came the late drama of MacKenzie's unstoppable winner to break Wrexham hearts and send Notts' fans home rejoicing.