Bangladesh lost three quick wickets and was struggling at 11-3 in pursuit of a victory target of 274 against South Africa after giving itself a sight of victory in the first test and another piece of history on Sunday.
Bangladesh took nine wickets for 88 runs in the last two sessions of the fourth day to bowl South Africa out for 204 in its second innings when it looked like the home team was about to build an unassailable lead.
But just as South Africa lost quick wickets to change the complexion of the match and raise Bangladesh's hopes of a first test win over the Proteas, so the Bangladesh batsmen slumped at the start of their chase to see it seesaw back in the home team's favor.
Openers Shadman Islam (0) and Mahmudul Hasan Joy (4) and captain Mominul Haque (2) were already out for Bangladesh having hardly helped in the chase. South Africa turned to its spin bowlers and not its pace men to open the bowling at the end of the day and it worked perfectly.
Keshav Maharaj collected 2-7 and Simon Harmer 1-4, and Bangladesh was in deep trouble after facing just six overs before bad light forced the umpires to call stumps.
South Africa was 116-1 in its second innings soon after lunch, 185 runs ahead and seemingly in position to build a huge advantage at Kingsmead and take the game away from Bangladesh.
But pace bowler Taskin Ahmed forced out South Africa captain Dean Elgar lbw for 64 for the crucial breakthrough and Bangladesh's other bowlers took that cue to run through the South Africans and keep the test alive.
Seamer Ebadot Hossain and spinner Mehidy Hasan Miraz claimed three wickets each as South Africa went from that position of strength at 116-1 to 204 all out.
The victory target of 274 meant Bangladesh had a realistic chance to win the opening test and follow up on its historic one-day series result last month, when it won a series in South Africa for the first time.
But Bangladesh's maiden test win over South Africa is in the balance.
Offspinner Harmer had Shadman caught at slip in the second over of the innings and left-arm spinner Maharaj sent Mahmudul and Mominul packing in the fifth over.