Having been stunned by a Joe Root-inspired England on Friday, the Proteas knew this was a game they had to win in order to harbour any realistic hopes of progressing from Group B.
They looked firmly on course when de Villiers smashed 64 from 29 deliveries in a total of 209 for five, but Mohammad Shahzad played another blistering cameo at the top of the Afghanistan order to threaten an upset.
Shahzad’s 44 from only 19 balls must have cast South African minds back to the carnage of Friday night, but their bowlers held their nerve on this occasion as Chris Morris finished with 4-27.
Faf du Plessis won the toss and elected to bat first, and although Hashim Amla tamely chipped Shapoor Zadran to mid-off for five, the skipper and Quinton de Kock smashed 66 from the powerplay.
Du Plessis made an entertaining 41 before being run out by an excellent throw from Mohammad Nabi, and de Kock followed when he edged Amir Hamza behind on 45.
Those wickets only brought de Villiers to the crease, however, and the 32-year-old produced a brutal innings – taking 22 off one over from 17-year-old spinner Rashid Khan before holing out off Nabi attempting to clear the rope for a sixth time.
JP Duminy and David Miller took 20 from the final over, but Shahzad treated Kagiso Rabada and Kyle Abbott with little respect as he single-handedly took 32 from the first two overs of the Afghanistan reply.
After hitting five sixes and three fours he was yorked by Morris, who also had Asghar Stanikazi caught behind for seven.
Gulbadin Naib hit 26 off 18 balls but his departure, after feathering Abbott behind, saw the associates struggle to keep up with the required rate and South Africa eventually emerged comfortable winners.