Marvin Emnes struck first before Nathan Dyer's impressive finish gave the visitors brief parity. Jonathan Grounds headed home from a free-kick before Leroy Lita doubled the lead just after the break.
Scott Sinclair narrowed the gap with a well-placed penalty before Ashley Williams levelled and Craig Beattie snatched victory at the death.
Emnes, previously on loan at the Liberty Stadium this season, opened the scoring after eight minutes, receiving a pinpoint pass from Lita and swivelling on the spot to wrongfoot Dorus de Vries and slot home from just inside the box.
Swansea did not let their heads drop and responded quickly following a move worthy of the best passing sides in the world.
Seb Hines made a rangy run into the opposition half before losing control of the ball. Swansea's quick counter-attacking dazed the Boro defence, four or five fluid passes finding Luke Moore on the right wing.
Dyer took Moore's cross unopposed to complete an excellent move from 12 yards out, side-footing past Jason Steele to level in the 14th minute.
The Swans' command of possession was at times eroded by sloppy defensive play and Lita found himself with a golden opportunity to take the lead. De Vries did well to parry a driven shot from just inside his penalty area.
Williams brought Scott McDonald down 25 yards from goal on 34 minutes. Barry Robson whipped in the resultant free-kick over a Swans wall that neglected to jump, finding Grounds in space to fire his first goal for Boro with a simple header.
The hosts' visibly improved after regaining the advantage and Hines was unlucky not to double the advantage before half-time.
After ghosting in to the far post, his strong header from a Tony McMahon corner was cleared off the line by Angel Rangel.
Tony Mowbray's teamtalk must have focused on exposing Swansea's frailty on the counter-attack as the hosts set out to nullify the fast one-touch football on display.
The improved mentality paid dividends when Robson's corner rebounded to Lita who headed home from close range just eight minutes after the restart.
Middlesbrough's joy was short lived when Julio Arca fouled Joe Allen on the hour mark. Sinclair stepped up to fire his penalty low to Steele's right to narrow the gap.
The daylight between the two sides was extinguished when Williams seized on Rangel's rebounding effort to tap in from six yards on 68 minutes.
Beattie snatched all three points with an injury-time winner, though, striking left-footed from 20 yards to break Middlesbrough fans' hearts.