Defending champion Federer defeated fellow Swiss Stanislas Wawrinka 6-3, 6-7 (4/7), 7-5, laboring two hours and 20 minutes to subdue his old friend.
Nadal, playing his fourth tournament since returning from a seven-month injury absence, outlasted the in-form Latvian Ernests Gulbis 4-6, 6-4, 7-5.
Federer had a golden opportunity to make his day much shorter, but serving for the match at 5-4 in the second set he was broken to love.
Unable to convert two break points in the following game he surrendered the set on a tiebreaker when he double-faulted on set point.
Wawrinka then gained the upper hand with a break in the third set, but Federer, winner of 17 Grand Slam titles, broke back and earned the decisive break in the final game as Wawrinka smacked a volley into the net.
"I think I was a little little lucky to come through it in the end," said Federer, who continued to say a sore back would not hinder his pursuit of a first title of 2013.
"Overall I'm very pleased that I was able to play today and play at a high level, which was important obviously at this stage of the tournament," Federer said.
Nadal is playing his first hard court tournament since left-knee tendinitis forced him out of the Miami Masters last March.
The same injury later sidelined him for seven months -- from a second-round defeat at Wimbledon until a comeback swing of three clay-court tournaments in Latin America in February that yielded two titles and a runner-up finish.
Nadal had admitted he did not know how his knee would hold up on hard courts, and in qualifier Gulbis he came up against a red-hot player who had won the Delray Beach title as a qualifier the week before Indian Wells and toppled two seeds en route to his meeting with the Spaniard.
"Always against Ernests it's a very difficult match," Nadal said. "He's a very, very aggressive player with a big serve."
Nadal saved the only break point he faced in the third set, and converted his only opportunity to break in the 11th game.
When he finally blasted a forehand winner on his third match point, Nadal gave a little leap of joy.
"I had to suffer a lot to win this match," he said.
A packed schedule on stadium court saw Nadal's match, the last of the day session, finish at at 9:47pm.
The night session was to feature world No. 1 Novak Djokovic taking on American Sam Querrey -- the last man to beat the Serbian, back on October 31 but before that Maria Sharapova was facing Sara Errani for a women's semi-final berth in the WTA event.
Earlier world No. 3 Andy Murray reached the quarter-finals with a 7-6 (7/4), 6-4 victory over Argentina's Carlos Berlocq.
Murray twice went down a break in the opening set, but he managed to quell his opponent to book a date with seventh-seeded Argentinian Juan Martin del Potro, who beat German veteran Tommy Haas 6-1, 6-2.
Sixth-seeded Czech Tomas Berdych defeated France's Richard Gasquet 6-1, 7-5 to advance to a quarter-final meeting with South African Kevin Anderson, a 6-3, 1-6, 6-4 winner over France's Gilles Simon.
Jo-Wilfried Tsonga kept the French flag flying with a 4-6, 7-5, 6-4 victory over Milos Raonic.