De Villers helped South Africa -- 1-0 up in the series -- score an imposing 439 for two after being sent in to bat.
That was the second highest total in one-day internationals and the highest between two Test nations.
De Villiers made 149 off 44 balls. His innings included 16 sixes, which equalled the record set by Rohit Sharma on the way to a score of 209 for India against Australia in Bangalore in 2013/14.
It was an astonishing display of power hitting mixed with finesse. Hashim Amla (153 not out) and Rilee Rossouw (128) put on 247 for the first wicket - a record partnership for any wicket for South Africa.
Rossouw was dismissed off the third ball of the 39th over. De Villiers promoted himself to number three and struck the ball out of the middle of his bat from the first delivery he faced, which he thrashed for four past bowler Jerome Taylor.
He went to fifty off 16 balls, one fewer than Sanath Jayasuriya's previous record for the fastest half-century, for Sri Lanka against Pakistan in 1995/96, then powered his way past Anderson's record.
South Africa went past their own previous highest total - the 438 for nine they scored in a record run chase against Australia on the same ground in 2005/06 - and Sri Lanka's world record of 443 for nine against the Netherlands in 2006 seemed set to fall.
But De Villiers hit the fourth ball of the final over from Andre Russell to deep cover and Amla could not score off the final two deliveries.
Amla's 153 not out was scored off 142 balls with 14 fours, while Rossouw hit his 128 off 115 balls with 11 fours and two sixes.
It was a maiden century for the left-handed Rossouw, who had the dubious distinction of having made five ducks in his previous ten one-day international innings.