Federer lost to Gulbis last month at the Rome Masters in his first clay-court match of the season, and the Latvian looked set to repeat the upset until the top-ranked Swiss found his stride in the second set.
"I think it's one of the toughest things in tennis if you lose against a player and you have to play against him in the next couple of weeks," Federer said. "I was very happy with the way I was able to return and mix up the game a bit, and at the end I thought it was a really great performance."
Gulbis took a 3-0 lead in the first set by converting his first break point when the onrushing Federer failed to return a ball driven at his feet. Federer then saved three set points before the Latvian converted his fourth with an ace. Gulbis then broke Federer again to start the second set.
After that it was all Federer, who broke back immediately when Gulbis sent a forehand long and then dominated the rest of the set, despite having to save three break points at 3-1.
In the third set, Federer used a perfectly judged backhand lob to break for a 2-1 lead and was never threatened on his serve the rest of the way. He converted his first match point with a forehand winner, shortly after a light rain started falling.
The third-seeded Murray, who turns 23 on Saturday, struggled to handle Ferrer's deep groundstrokes and lost his serve twice in each set, growing visibly frustrated throughout the 2-hour, 11-minute match.
The 23-year-old Monfils was playing his first clay tournament of the season and was outclassed by a dominant Nadal, who has not lost a service game in his three matches in Madrid.
The second-seeded Spaniard pressured Monfils' serve from the start and broke to go up 3-1 in the first set after the 12th-seeded Frenchman double-faulted.
Another unforced error by Monfils gave Nadal a second break before he comfortably served out the set.
Monfils earned his only two break points of the match when leading 2-1 in the second, but Nadal won the next four points and cruised through the rest of the set.
Nadal is 13-0 on clay this season, winning titles in Monte Carlo and Rome. He is looking for a record 18th Masters title. Nadal and Andre Agassi have won 17 each, with Federer at 16.
"In the first set I played at a very high level," said Nadal, who compared the high-altitude conditions of Madrid with his previous two tournaments.
"In Monte Carlo I played one of the best tournaments of my life on clay. In Rome, I played very well, too. This is the toughest tournament for me. The conditions are the most difficult of the year for me on clay, but I'm fine. Yesterday I played quite well [against John Isner], today better. I'm very happy," he said.
Almagro broke Melzer twice to close out the first set before racing to a 3-0 lead in the second with some solid serving.
The 24-year-old had another break before securing his first ATP Masters semifinal spot.
Williams, who will climb to the second ranking behind sister Serena Williams next week, has now won all four of her meetings with the Australian.
"Her game plan seemed to be to just attack everything," Williams said. "She played well. But there were some key points, and at the end, from 4-3, I felt like I went on autopilot, so it felt good."
Despite the defeat, Stosur's recent play is expected to lift her to seventh in the rankings next week, the highest held by an Australian since Wendy Turnbull in 1985.
It was the Czech player's first win over Petrova in four meetings.