Mahela Jayawardene (77) and Thilan Samaraweera (37) caused more problems for the hosts, adding 116 runs off 106 runs for the fourth wicket before Wayne Parnell dismissed them in consecutive deliveries. Both Parnell and Dale Steyn picked up three wickets.
South Africa started the chase on a rocky note after medium pacer Angelo Matthews rattled opener Hasim Amla's (2) stumps in the third over.
Graeme Smith (58) and Jacques Kallis (31) started the repair act but it was mystery spinner Mendis who turned the match on his heels picking up the two batsmen for his three for 30.
South African batsmen completely failed to read Mendis and the Sri Lankan simply kept the host guessing with his well disguised deliveries.