MADRID (AP) — Roger Federer will meet Rafael Nadal in a second consecutive Madrid Masters final after beating David Ferrer of Spain 7-5, 3-6, 6-3 on Saturday.

Nadal came from behind to beat Spanish countryman Nicolas Almagro 4-6, 6-2, 6-2 in the first semifinal.

Last year, Federer beat then No. 1-ranked Nadal in the final for his second Madrid title.

Federer said he had not expected to wait a year to play Nadal again.

"After playing here last year you figured, maybe the French, Wimbledon or down the stretch in America at the latest we'll face off again, but it just never ended up happening," Federer said. "It shows how incredible our last few years have been and I think it's exciting for tennis and for us obviously that we can face off again."

Nadal, assured of returning to No. 2 in the rankings with Saturday's win, is aiming for his second title in Madrid.

Nadal is 14-0 on clay this season, winning titles in Monte Carlo and Rome. He has lost only two sets during that run, and is looking for a record 18th Masters title, including his previous Madrid title in 2005. Nadal and Andre Agassi have won 17 each, with Federer at 16.

"Whatever happens (in the final) I can say that this season has been very good. I'd say it's been perfect — or nearly perfect," Nadal said.

Federer, who'd beaten Ferrer in all nine of their previous meetings, served superbly in the first set, losing only two service points and sending 77 percent of his first serves in.

By contrast, Ferrer's serve was under pressure from the start and the Acapulco champion and Rome runner-up had to save two break points on his second service game.

Although Federer was in control, the Spaniard saved a further three break points at 4-4.

However, a forehand winner down the line gave the top-ranked Swiss another break point at 5-5 and this time he converted it when Ferrer hit the net. Federer then served out the set.

In the second set, Federer's serve faltered. Ferrer took the third of three break points to go up 3-1 before holding serve to take the second set.

Both players racked up several unforced errors in the third set. Eventually Federer carved out a break point with a crosscourt winner followed by a long forehand from Ferrer, and the defending champion took the chance before serving for the match.

The second-seeded Nadal made a string of early unforced errors, dropping serve three times in the opening set.

"I made a lot of mistakes in the first set and Almagro was playing really well," said Nadal. "Because he was attacking me a lot I tried to play safer in the first set — that was a mistake."

Almagro appeared on course to finally beat Nadal in their sixth meeting with a flurry of forehand winners.

"I've played at a really high level all week," said Almagro. "And I had the best clay-court player in the world against the ropes."

Nadal, a four-time French Open champion, broke back in the second game. Almagro won the next game to love when Nadal hit a backhand into the net, then served to go up 4-1.

Nadal gave a trademark shout and pump of the fist as he broke back again, although the unseeded Almagro coolly saved two break points at 5-4 to close out the set.

In the second set, Nadal's groundstrokes started finding their target and he broke twice as Almagro made 15 unforced errors.

Almagro sent a forehand wide in the second game of the decider to hand Nadal the key break he needed before he served out the match.

Earlier, Venus Williams reached her first Madrid Masters final by beating Shahar Peer of Israel 6-3, 6-0.

Williams has a 12-1 record on clay this year after beating Samantha Stosur and Vera Zvonareva this week.

"I've played so many different players with so many different styles — some play more clay-court tennis, others just hit the ball really hard," Williams said. "So just to come out on top so far is good, and I'm happy with my form and I'm going to try to keep that form tomorrow."

The fourth-seeded American will play Aravane Rezai of France after Lucie Safarova of the Czech Republic retired from the second women's semifinal. Safarova withdrew because of a thigh injury at the start of the second set.