Four-times Indian Wells champion Federer last played Nadal a year ago, also at Indian Wells, when the Swiss won their semi-final 6-3 6-4, though the Spaniard leads overall in their meetings 18-10.
Australian Open champion and top seed Djokovic, who began his "evening" match in the early hours of Thursday morning local time, advanced with a 6-0 7-6 victory over big-serving American Sam Querrey.
Third seed Murray needed almost two hours to beat Carlos Berlocq of Argentina 7-6 6-4 and will next face seventh-seeded Juan Martin Del Potro of Argentina, who overpowered German Tommy Haas 6-1 6-2.
Federer appeared to be in cruise control serving for the match at 5-4 in the second but was broken to love before his opponent went on to clinch the set in a tiebreak 7-4.
The players traded early breaks of serve in the third before Federer sealed the win by again breaking Wawrinka in the 12th game when his compatriot and good friend netted a forehand.
"Today it was extremely close again," Federer said after coming out on top of a fluctuating contest lasting two hours 20 minutes. "He usually plays me pretty good actually overall.
"Okay, I should maybe close it out in the second set, but he did well to stay in it. At the end, I don't know what gets me through.
"Maybe it's the experience or maybe I'm a bit more calm in those moments. I'm not sure. Today I think I was a little lucky to come through it in the end," added Federer, who improved his record against Wawrinka to 13-1.
Nadal, playing his first hardcourt event in almost a year, lost a closely contested opening set to Gulbis when he hit a forehand long on the first break point of the match.
However, the left-hander steadily upped his game to take control of the next two sets as the two players treated a packed house at the Indian Wells Tennis Garden with a brilliant display of shot-making.
Twice champion Nadal broke Gulbis for the final time in the 11th game of the third set when his opponent dumped a forehand into the net, then served out in the 12th, sealing victory with a whipped forehand winner down the line.
"Always against Ernests it is very difficult, he is a very aggressive player with a big serve," Nadal, who is continuing his comeback after being sidelined for seven months last year by a left knee injury, said courtside.
"I played not my best in the first set ... but I am through and that's the most important thing."
Top seed Djokovic stormed through his opening set against local favourite Querrey in only 22 minutes before winning a closely contested second set 8-6 in the tiebreak.
"I started off the match incredibly well, having waited six hours on site to play my match," Djokovic said. "He played well in the second and it could have gone either way. I am really thrilled to go through."
US Open champion Murray, a losing finalist here in 2009, advanced with a straight sets victory that featured seven service breaks, plenty of grunting by the Argentine and a series of protracted games.
"It was tough," said the Scotsman, who took the first-set tiebreak 7-4. "He started well and he was playing very aggressive. He had a lot of chances in the first set.
"And then the second set was kind of the other way around. I had a lot of chances, but it was still tight. All the games were pretty close, a lot of long games and longish rallies."
In other matches, sixth-seeded Czech Tomas Berdych eased past Frenchman Richard Gasquet 6-1 7-5 while eight-seeded Jo-Wilfried Tsonga came from a set down to beat Canada's Milos Raonic 4-6 7-5 6-4.
BNP Paribas Open results
1-Novak Djokovic (Serbia) beat 23-Sam Querrey (U.S.) 6-0 7-6(6)
5-Rafa Nadal (Spain) beat Ernests Gulbis (Latvia) 4-6 6-4 7-5
7-Juan Martin Del Potro (Argentina) beat 19-Tommy Haas (Germany) 6-1 6-2
3-Andy Murray (Britain) beat Carlos Berlocq (Argentina) 7-6(4) 6-4
2-Roger Federer (Switzerland) beat 18-Stanislas Wawrinka (Switzerland) 6-3 6-7(4) 7-5
6-Tomas Berdych (Czech Republic) beat 10-Richard Gasquet (France) 6-1 7-5
8-Jo-Wilfried Tsonga (France) beat 17-Milos Raonic (Canada) 4-6 7-5 6-4
Kevin Anderson (South Africa) beat 13-Gilles Simon (France) 6-3 1-6 6-4
- Sports & Recreation
- Carlos Berlocq