Djokovic, a two-time champion in the Spanish capital, has not won a deciding set this year and, having previously suffered final set losses to Martin Klizan, Dominic Thiem and Taro Daniel, the 30-year-old Serb’s disappointing run continued.
Edmund claimed the decisive break in the eighth game of the decider before coolly closing out his service game to love for his 14th match win of the year.
“It was a great experience to beat Novak, he is a legend of the game,” Edmund told Sky Sports. “It’s time to try and beat these guys and I was pleased how I managed my game.”
The world number 22 will face eighth-seeded Belgian David Goffin in the third round.
Djokovic, who has been hampered by a long-term elbow injury, has not reached the last eight of a tournament since last year’s Wimbledon.
He has struggled on his return to clay ahead of the French Open, having suffered early exits in Monte Carlo and Barcelona last month.
“Look, there are obvious things that are not working well for me,” the 12-times grand slam champion said.
“But I have to keep working on them and pray that and hope that my game will get stronger, get better as definitely as the matches go the distance.”
World number one Rafa Nadal recorded his 20th straight win on clay as he began his quest for a sixth title in Madrid, defeating Frenchman Gael Monfils 6-3 6-1.
Nadal, who received a bye in the opening round, fired 17 winners and went on to lose just one of 13 service points in the second set to seal the victory.
The 31-year-old Spaniard has now won 48 consecutive sets on the surface, two sets shy of breaking American John McEnroe’s record of 49 straight sets on a single surface in 1984.
World number seven Dominic Thiem and sixth-seed Kevin Anderson joined Nadal and Edmund in the last-16 but home favourite Feliciano Lopez, who will become tournament director next year, was beaten by Argentine Diego Schwartzman.