(CNN) -- Roger Federer came from behind to beat Tomas Berdych to win his third Madrid title and equal Rafael Nadal's record of 20 Masters titles.
Federer won 3-6 7-5 7-5 to continue a rich vein of form that has seen him lose just three times in 48 matches over the last eight months and ensure the Swiss will take Nadal's No. 2 ranking on Monday.
The 16-time grand slam champion's ranking rise is significant because Federer could avoid playing No. 1 Novak Djokovic and Nadal until the final of the French Open -- the second major tournament of the tennis season, which starts on May 28.
"It's been a great spell and I couldn't be more happy right now coming off a break winning right away," Federer following his first ATP Tour tournament after a third-round exit in Miami in March.
The Swiss star struggled in his opening match before beating big-serving Milos Raonic in three sets, and grew in confidence on the controversial blue clay while Nadal and Djokovic fell by the wayside.
"I was worried about getting through the first round and was not even thinking about who was going to be my second round opponent," Federer said after claiming his fourth title this year and 74th overall.
"I was very realistic coming into this event. Confidence is coming back. I was away and it didn't take much for me to get my confidence back, especially now that I've been playing so well for the past nine months."
Berdych got off to the best possible start in Sunday's final, breaking Federer straight away for a 2-0 lead.
The Czech had a chance to secure the first set when Federer was serving, before Berdych closed it out on his own serve in the ninth game.
However Berdych's service let him down at a key point in the second set's 12th game when he double-faulted, allowing Federer to pull level.
Berdych squandered two break points at the start of the third set, and Federer sealed victory on his fourth match point after two hours and 38 minutes.
Federer has now lost just four times to Berdych in 15 meetings, though the Czech player had won three of their past five matches, including a win in the 2010 Wimbledon quarterfinals.
The tennis circuit shifts to Rome next week and a return to the reddish clay for the rest of the clay court season.
That is likely to be a welcome relief for Nadal and Djokovic, who had been vocal in their criticism of Madrid's blue clay surface, particularly after their shock exits at the hands of Fernando Verdasco and Janko Tipsarevic respectively.