The South African No 1 led 5-3 in the first set but crumbled when trying to serve it out. Berdych then won the tie-break 7-5 and went on to win in straight sets.
Berdych advances to the quarter-finals where he will play the winner of the match between third seeded Brit Andy Murray and 16th seeded Frenchman Gilles Simon.
The loss ends a good week on clay for world No 27 Anderson, who had earlier wins over Canada's Jesse Levine (6-2, 6-2) and Argentine Juan Monaco 7-6 (7/5), 3-6, 6-4.
Results from the sixth day of the ATP Madrid Masters and WTA Madrid Open on Thursday (x denotes seeding):
Men
Third round
Jo-Wilfried Tsonga (FRA x7) bt Fernando Verdasco (ESP) 4-6, 6-3, 6-2
Tomas Berdych (CZE x6) bt Kevin Anderson (RSA) 7-6 (7/5), 7-5
Rafael Nadal (ESP x5) bt Mikhail Youzhny (RUS) 6-2, 6-3
Pablo Andujar (ESP) bt Daniel Gimeno (ESP) 5-5 - retired
Kei Nishikori (JPN x14) bt Roger Federer (SUI x2) 6-4, 1-6, 6-2
David Ferrer (ESP x4) bt Tommy Haas (GER x13) 7-5, 4-6, 6-4
Women
Third round
Serena Williams (USA x1) bt Maria Kirilenko (RUS x13) 6-3, 6-1
Kaia Kanepi (EST) bt Daniela Hantuchova (SVK) 6-3, 6-4
Maria Sharapova (RUS x2) bt Sabine Lisicki (GER) 6-2, 7-5
Ekaterina Makarova (RUS) bt Marion Bartoli (FRA x14) 6-3, 6-2