Cardiff City returned to the play-off places after two own goals damaged Bristol City's hopes of survival.
The Bluebirds took the lead when Joe Mason's low cross was turned into his own net by Stephen McManus.
Jon Stead equalised early after the break from Richard Foster's accurate cutback before Cardiff goalkeeper David Marshall twice denied the home side.
And with two minutes remaining, Mason's shot deflected in off Kalifa Cisse to give Cardiff a first win in four games.
The result was cruel on relegation-threatened Bristol, who looked the most likely team to grab a late winner in late flourish.
Marshall was at full stretch to save substitute Sean Davis's well controlled 20-yard curler before saving Brett Pitman's effort following a flowing home move.
It was a far cry from the home side's performance in the first half when they looked toothless in rare attacks as Cardiff dominated.
Half chances fell to lone striker Kenny Miller, the first an early half volley and then a driving far-post volley, but both efforts were comfortably saved by home goalkeeper David James.
His opposite number Marshall was equally solid in saving Pitman's shot on the turn near the edge of the box as the first half ended with far greater urgency.
Pearson was then guilty of wasting a glorious opportunity for Bristol when he blasted over from 12 yards, and Cardiff took full advantage by scoring a minute later in first-half stoppage time.
The visitors moved the ball slickly from left to right, eventually finding Mason inside the Bristol City box on the right flank.
He fried a low cross across the face of goal and McManus could only slice the ball into his own net at the front post as he attempted to clear.
Cardiff began the second half with purpose as Aron Gunnarsson forced two fine saves from James within a minute.
But just as the pressure began to build, Bristol equalised with a goal of efficient simplicity.
A long ball down the right flank caught out Cardiff left-back Andrew Taylor allowing Richard Foster to attack the byline.
His low cross fell perfectly into Stead's path and his first-time strike sneaked in between Marshall's legs and the front post.
Marshall then kept Cardiff in the game with two fine saves before another huge slice of luck gave the visitors an unlikely win.
Peter Whittingham found Mason in space on the left side of the Bristol box and his attempted low cross left James stranded as he flicked off Cisse's heel into the net.