The Briton, who was thrashed 6-3, 6-1 by the Croatian in the Dubai Championships in 2015, performed poorly in the first set and after recovering in the second he then surrendered his serve in the seventh game, giving Coric three break points.
The first set went with serve until the Serb broke his opponent to take it 6-4 but Lopez was in no mood to surrender and at 5-5 in the second he had a chance to break back at 0-30.
In his first match since changing his coaching team in an attempt to change fortunes following a hard start to the year, Djokovic was taken to three sets by Spain's Nicolas Almagro in a match which lasted for two and a quarter hours.
Wednesday's match was his first since splitting with longtime coach Marian Vajda. The top-ranking Spaniard and four time victor in Madrid needed just an hour and 12 minutes before knocking out Australian 16 seed Nick Kyrgios 6-3, 6-1.
Big-serving and fifth seed Canadian Milos Raonic, who finished runner-up last week at Istanbul, continued his good form on clay with a 6-4, 6-4 win over Gilles Muller of Belgium.
Serbia's Novak Djokovic reacts after defeating Spain's Nicolas Almagro in their second round match of the Mutua Madrid Open tennis tournament in Madrid, Spain, 10 May 2017.
Because since that French Open victory, Djokovic has hit the wall. But, as I said, it was quite a solid performance from both players, high quality.More news: Cornyn removes self from consideration for Federal Bureau of Investigation director role
In the sixth game, Djokovic drilled a deep return rattling an error to earn the first break point 20 minutes into the match.
Fognini dictated much of the play but Nadal ground out the first set and showed resilience to break for the match immediately after dropping his serve in the decider.
Djokovic had to battle back from 3-0 down in the deciding set to book his place in the last 16.
Kei Nishikori recovered from a terrible start to beat Argentine Diego Schwartzman 1-6, 6-0, 6-4.
"However, I have been able to train for a couple of hours today and I am better than yesterday and the doctor has told me it is nothing serious", said Nadal, who admitted he had "never had anything like this before". "I can take a lot of positives from today".
Djokovic had to withdraw from the Miami Open because of an elbow injury.
On the women's side, defending champion Simone Halep of Romania cruised past Anastasija Sevastova of Latvia 6-2 6-3 to reach her third Madrid Open final.
Kuznetsova will face the victor of an intriguing clash between top seed Angelique Kerber and Eugenie Bouchard, who dumped out Maria Sharapova in thrilling fashion on Monday.