* Federer beat good friend Wawrinka for 13th time
* Nadal rallies to beat Latvian Gulbis
* Murray overcomes grunting Argentine Berlocq (Updates with Djokovic win)
INDIAN WELLS, California, March 13 (Reuters) - Defending champion Roger Federer and Rafa Nadal set up a mouth-watering showdown in the BNP Paribas quarter-finals after they battled through tricky fourth-round encounters on Wednesday.
Federer rode his luck to scrape past fellow Swiss Stanislas Wawrinka 6-3 6-7 7-5 while Nadal had to come from a set down to beat talented Latvian qualifier Ernests Gulbis 4-6 6-4 7-5.
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 Novak 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 Andy 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 told reporters 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.
FIRST HARDCOURT EVENT
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."
U.S. 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. (Reporting by Mark Lamport-Stokes; Editing by Peter Rutherford/Amlan Chakraborty)