Novak Djokovic staved off a huge upset and Rafael Nadal was made to battle in their opening matches at the Madrid Masters on Wednesday.

In his first outing since splitting with his long-time coaching team, Djokovic had to dig deep before eventually dispatching Nicolas Almagro 6-1, 4-6, 7-5 in his second-round tie.

Nadal was also taken to three sets before seeing off Fabio Fognini 7-6 (7/3), 3-6, 6-4.

“My level of tennis was not so high today, but I managed to make it through the first round. It was uncomfortable. It was a really awful day. it was really windy,” Nadal said.

“It’s true I’ve been through some difficult days with the issue of my ear (infection), which cost me a little bit of general stability.

“I feel a little bit strange. But it’s something that is getting better day after day.”

Djokovic arrived in the Spanish capital without a coach after bringing to an end a 10-year relationship with Marian Vajda in an attempt to reverse his dramatic slump in form over the past year.

And after a blistering start, Djokovic had to battle back from 3-0 down in the deciding set to book his place in the last 16 on Thursday against Feliciano Lopez, who beat Gilles Simon 6-3, 3-6, 7-6 (7/3).

“I’m still finding my way to that consistency level and quality of tennis that I’m looking for,” said Djokovic.

“I’m aware that I’m not playing at my best, but I’m definitely believing in myself and the process.

“Eventually the game will come together.”

Wawrinka eliminated

The early signs were positive for the 12-time Grand Slam champion as despite losing his service in the opening game, he broke straight back to start a six-game streak to romp through the first set.

Almagro had taken just one set from four previous meetings with Djokovic, but willed on by a partisan home crowd, he held on to his serve despite more pressure from the Serb in the first game of the second set.

The Spaniard had to save two break points to hold for 4-3 and then took his chance to break with Djokovic serving to stay in the set.

The world No 75, who dipped down to the challenger tour after losing in the first round of the Australian Open earlier this year, then looked set for a monumental upset as he raced into a 3-0 lead in the decider.

However, a mixture of Almagro nerves and Djokovic upping his intensity saw the world No 2 react immediately to level at 3-3.

More Almagro errors flowed to give Djokovic another break for 6-5 and he served out for the match. He could meet Nadal in the semi-finals.

The home favourite needed 90 minutes to complete the opening set, finally finishing off Italian Fognini on a second match point after being broken while serving for victory.

The 14-time Grand Slam champion now faces Nick Kyrgios in the third round.

Controversial Australian Kyrgios cruised past American Ryan Harrison 6-3, 6-3.

Doubles partner Benoit Paire upset third seed Stan Wawrinka 7-5, 4-6, 6-2 after the Swiss had served for the first set.

Fifth seed Milos Raonic is into the last 16 as he got the better of a battle between two big servers, 6-4, 6-4 over Gilles Muller.

Kei Nishikori recovered from a dreadful start to beat Argentine Diego Schwartzman 1-6, 6-0, 6-4 and faces David Ferrer next.

In the WTA Madrid Open, Canadian Eugenie Bouchard advanced when top seed Angelique Kerber retired with a hamstring injury trailing 6-3, 5-0 in their third-round match.

Kerber will take over the top ranking on Monday as a result of earlier Madrid results. The match lasted less than an hour.

Brief results:

Men’s draw

2nd round:

Benoît Paire (FRA) bt Stanislas Wawrinka (SUI x3) 7-5, 4-6, 6-2

Milos Raonic (CAN x5) bt Gilles Muller (LUX) 6-4, 6-4

Nick Kyrgios (AUS x16) bt Ryan Harrison (USA) 6-3, 6-3

David Ferrer (ESP) bt Jo-Wilfried Tsonga (FRA x10) - injury walkover

Tomas Berdych (CZE x11) bt Robin Haase (NED) 7-6 (7/5), 6-3

Novak Djokovic (SRB x2) bt Nicolas Almagro (ESP) 6-1, 4-6, 7-5

Kei Nishikori (JPN x6) bt Diego Schwartzman (ARG) 1-6, 6-0, 6-4

Borna Coric (CRO) bt Pierre-Hugues Herbert (FRA) 7-5, 6-4

Grigor Dimitrov (BUL) bt Ivo Karlovic (CRO) 6-3, 7-5

Pablo Cuevas (URU) bt Nicolas Mahut (FRA) 5-7, 6-4, 6-4

Rafael Nadal (ESP x4) bt Fabio Fognini (ITA) 7-6 (7/3), 3-6, 6-4

Feliciano Lopez (ESP) bt Gilles Simon (FRA) 6-3, 3-6, 7-6 (7/3)

Alexander Zverev (GER) bt Marin Cilic (CRO x7) 6-7 (3/7), 6-3, 6-4

Women’s draw

3rd round:

Anastasija Sevastova (LAT) bt Lara Arruabarrena (ESP) 7-5, 6-2

Svetlana Kuznetsova (RUS x8) bt Qiang Wang (CHN) 6-4, 7-5

Eugénie Bouchard (CAN) bt Angelique Kerber (GER x1) 6-3, 5-0 retired

Kristina Mladenovic (FRA x14) bt Océane Dodin (FRA) 6-2, 6-1

Sorana Cirstea (ROM) bt Misaki Doi (JPN) 7-5, 3-6, 6-1

Coco Vandeweghe (USA) bt Carla Suárez (ESP) 5-7, 6-4, 7-5

Simona Halep (ROM x3) bt Samantha Stosur (AUS x16) 6-4, 4-6, 6-4

Kiki Bertens (NED) bt Irina Begu (ROM) 6-1, 7-5