A goal two minutes from time by Adrian Forbes clinched victory for the Swans as they moved to the top of the League Two table.
It was an end-to-end match as both sides searched for the victory but the Swans fully deserved their success.
Gary Monk was missing from the home side's starting line-up after being knocked unconscious in the 1-1 draw at Chester last Saturday so Alan Tate came in for a rare start under Kenny Jackett.
Neil Mackenzie took the place of the suspended Adam Murray for the Stags.
The visitors were first on the attack and after only 80 seconds Colin Larkin lifted a close-range shot over the bar and when the ball was cleared to the other end Lee Trundle forced Town keeper Kevin Pilkington to push his shot behind for a corner.
Both goals had narrow escapes in a hectic opening with Pilkington only grabbing the ball at the second attempt with Andy Robinson waiting to pounce and then Larkin beat Andy Gurney down the left but his angled shot was safely gathered by Willy Gueret in the home goal.
In the 15th minute Robinson set up Sam Ricketts with a neat back-heel but the full-back's shot was deflected behind by Dave Artell as it seemed destined for the far corner.
Five minutes before the break Robinson shot wide from a good position and then Paul Connor clipped a shot past Pilkington but Craig Woodman raced back to hack it away from under the crossbar.
It was "as you were" when the second half started with a Brad Maylett shot hitting the crossbar in the 48th minute with Pilkington beaten.
Midway through the half a rare Mansfield breakaway saw Larkin beat the offside trap but as he tried to curl his shot round Gueret, the Frenchman stretched out an arm to save.
Swansea used all their substitutes as they kept working hard for the victory. With time ticking away they got the breakthrough their performance deserved when Forbes was waiting unmarked at the far post to head a centre from Leon Britton across Pilkington and into the corner of the net.
There were a couple of chances to double the score in a frantic finale but Forbes' effort proved to be enough.