Wes Brown's last-minute header earned Sunderland the victory at Loftus Road - just when it looked like they had gifted QPR a point.
Sunderland were 2-0 up and in control courtesy of goals from Nicklas Bendtner and Stephane Sessegnon when slack defending allowed QPR back into the game.
QPR had been toothless for the best part of an hour but launched an unlikely comeback when Heidar Helguson bundled Alejandro Faurlin's cross into the Sunderland net.
The Icelandic striker then provided the knock-down for Jamie Mackie to draw QPR level before Rs goalkeeper Paddy Kenny kept it that way with two magnificent saves.
But Brown clinched the victory for Sunderland with a header from Kieran Richardson's corner which Faurlin could not keep out, despite his best efforts on the line.
There were no pre-match indications of the drama to come, with both sides struggling for wins and QPR the lowest-scoring team in the Barclays Premier League.
After an hour it was easy to see why and manager Neil Warnock will need to sharpen QPR's cutting edge in the January transfer window if they are to move away from the relegation zone.
For Sunderland, Martin O'Neill has now overseen two wins from his first three matches in charge and he elicited an immediate reaction from Bendtner.
The Danish striker and midfielder Lee Cattermole both returned to the side for the first time since being arrested on suspicion of criminal damage.
O'Neill told on-loan Bendtner the best way to impress him would be to behave himself off the pitch and score more goals on it, challenging him to "win us some football matches".
And Bendtner set the ball rolling. He had already hit the post with a stooping header from Sebastian Larsson's free-kick when he put Sunderland ahead.
Sunderland counter-attacked, won a corner and Bendtner muscled past Matthew Connolly to meet Richardson's delivery from the the right-hand side with a powerful near-post header.
QPR's confidence drained after conceding such a simple goal. Passes were woefully misplaced, corners needlessly conceded and the patience of the home support was growing thin.
Sessegnon came close to doubling Sunderland's lead when he cut inside and saw his shot deflected over the bar while Danny Gabbidon conceded a corner from Richardson's weak cross-shot.
Faurlin combined well with Armand Traore down the left to force a corner but Helgusson's header went straight at Sunderland keeper Keiren Westwood.
Warnock sent on Adel Taarabt at half-time and that enlivened QPR, who came close to drawing level when Helguson's header was cleared off the Sunderland line by Titus Bramble.
But Gabbidon then misjudged a header and Sessegnon latched onto the ball and rounded Kenny to fire Sunderland 2-0 and seemingly seal the win.
But from nowhere QPR hit back. Westwood and Brown failed to deal with Faurlin's cross and Helguson bundled it home.
Four minutes later, Helguson turned provider, heading the ball down for Mackie to score his first goal since breaking his leg in January.
Having battled their way back to level terms, QPR had Kenny to thank for keeping it that way until the last minute with two magnificent acrobatic saves.
Craig Gardner had Bendtner available but tried to curl his shot inside the far post - only to see Kenny palm his effort around the post.
Sunderland still had more pace about them in attack and Sessegnon almost picked out Richardson with a reverse ball before Phil Bardsley was denied by more Kenny acrobatics.
But neither Kenny nor Faurlin on the line could do anything to deny Brown, who met Richardson's corner and directed his header inside the far post.