The home side went ahead after only 7 minutes. Steve Melton fouled Leon Britton inside the box, and James Thomas scored from the penalty.
But City equalised a minute later through Stuart Elliott. He capitalised on a Lee Jenkins error to fire past Neil Cutler.
City took the lead in the 25th minute when Martin Reeves marked his last game on loan with a goal, latching onto a backpass and chipping Cutler.
But the lead was lost just before half time as Swansea restored parity with another penalty. This time Justin Whittle was penalised for handball inside his own area, and Thomas again scored from 12 yards.
Shortly after half time Swansea went back in front when Dean Keates gave away a free kick. Martinez took the dead ball, Nugent headed on and Lenny Johnrose got on the end of it to beat Alan Fettis.
Fettis was beaten again in the 57th minute as James Thomas sealed his hat-trick. Jon Otsemobor had lost possession, and Thomas lobbed the City keeper to make it 4-2.
The win saved them from relegation, despite Exeter winning 1-0 against Southend. The Grecians go out of the Football League with Shrewsbury, as Yeovil Town and the Conference play-off winners replacing them.
Elsewhere, Rushden & Diamonds clinched the Division 3 championship, with Hartlepool and Wrexham also going up. Scunthorpe play Lincoln in the play-offs, while Bury take on Bournemouth. Northampton, Mansfield, Huddersfield and Cheltenham drop down from Division 2.
City line-up (4-4-2): Fettis; Otsemobor (Burton 69), Whittle, Joseph, Smith; Reeves, Delaney, Keates, Melton; Burgess (Webb 62), Elliott.
Un-used subs: Musselwhite, Anderson, Regan.