Djokovic stormed through the second set on the Manolo Santana court and, at his best, might never have looked back, but he spurned three break points early in the decider and Edmund pounced to win 6-3, 2-6, 6-3.
This was only the third time Edmund had beaten a player ranked as high as world number 12 and in terms of status, Djokovic is by far his biggest scalp yet.
“Kyle was just better in the decisive moments and I was a bit unlucky,” Djokovic said afterwards. “Definitely Kyle is playing the best tennis of his life.”
Edmund’s barnstorming forehand wreaked havoc throughout and it was no surprise when the winners tally showed the Briton 30-11 ahead by the time Djokovic sent a final backhand long.
Belgium’s David Goffin, whom Edmund lost to on clay during Britain’s run to Davis Cup glory in 2015, now awaits in the last 16.
Djokovic, meanwhile, is left reviewing another early exit ahead of the French Open later this month. The 30-year-old has shown glimpses of his best this week but those moments have still been few and far between.
“It’s not the end of the world,” Djokovic said. “Obviously, I’ve played this sport for so many years and had a bunch of success. I try to always remind myself and be grateful for that.
“At the same time, nobody is forcing me to play this sport. I do it because I like it. That’s where I draw my strength.”
Serving to stay in the opening set, Djokovic played a dreadful game and, with three set points, Edmund took the first when a driving return caught the line.
Djokovic seemed hungrier in the second and was in charge from the moment he broke Edmund to lead 2-0. Another break at 4-2 meant the match would be settled in a third set.
Edmund came back from 0-40 down in the fourth game and it proved a pivotal moment as he then broke himself for 5-3 when a Djokovic forehand looped long.
Djokovic was cursing his luck after a couple of awkward bounces but Edmund would not be distracted, serving out a superb victory after an hour and 42 minutes.
Earlier, Simona Halep maintained her charge towards a third consecutive Madrid Open title as she eased past Kristyna Pliskova 6-1 6-4.
Halep will now play Pliskova’s sister Karolina for a place in the last four.
Caroline Wozniacki, however, is out. The Dane could have ursurped Halep as world number one by winning the title this week but instead endured a shock 6-2 6-2 defeat to Kiki Bertens.