England, at lunch on the fourth day, were 130 for four - still needing a further 189 runs to make South Africa bat again.
Alastair Cook was 46 not out and Ian Bell one not out.
England were 12 minutes away from getting through the session without losing a wicket when nightwatchman James Anderson, who'd surviving a painful blow on the head from Dale Steyn while batting for 111 minutes in all, was out to the fast bowler for a Test-best 34.
South Africa-born Pietersen leg-glanced and off-drove Steyn for fours off his first two balls and then square-drove Jacques Kallis for another boundary.
But the all-rounder had the last laugh when a delivery from the paceman got big on Pietersen, trying to leave the ball too late, and took the shoulder of the bat with wicket-keeper Mark Boucher taking his eighth catch of the match.
What Anderson, who'd toiled away for 44 overs in the field with the ball before his lengthy innings made of the star batsman's five-ball and five-minute stay was anyone's guess.
Monday marked the 27th anniversary of the most famous recovery in Headingley history when, inspired by Ian Botham and Bob Willis, England beat Australia in an Ashes Test after being following-on.
Although England were batting to save this game rather than win it, few would have given much hope of similar heroics when they resumed Monday on 50 for two.
Fast bowler Makhaya Ntini had removed opener Andrew Strauss and captain Michael Vaughan, leading the side in a Test match for the 50th time, with two superb deliveries which saw both batsmen eding to wicket-keeper Mark Boucher before stumps on Sunday.
Opener Cook was 23 not out and Anderson unbeaten on nought after South Africa had piled up 522 in their first innings, a total built around AB de Villiers's 174 and Ashwell Prince's Test-best 149.
The left-handed England pair got through the first hour, an encouraging sign for the home side as they tried to replicate the Proteas' feat in batting for two days to draw the first Test of this four-match series at Lord's.
Indeed paceman Anderson, who bats left-handed but bowls right, cover-drove Paul Harris off the backfoot and frontfoot off successive deliveries for four in the spinner's first spell of the match.
Then a single off the same bowler saw him surpass his previous Test-best of 28, made against New Zealand at Trent Bridge in June.
However, Anderson was then struck two painful blows in as many balls by fast Steyn, operating from around the wicket.
First he was hit on the right wrist and then more worryingly, after several minutes of treatment, the next ball saw him turn his head way before being struck on the side of the face, the ball forcing the grille onto his cheek.
Anderson immediately fell to the ground but was back on his feet after five more minutes of treatment and, fortified by a new helmet, he carried on.
But a stand of 59 ended when he was lbw to Steyn.