“Terribly sorry to the spectator, first of all,” he told reporters afterwards, adding it was “100 per cent an accident” and that he wished to apologise to the fan in person.
“The ball was bouncing back, so I tried to, like, just hit it anywhere, so it doesn’t go out from the court,” he said.
It had been a physical battle before Medvedev’s disqualification.
Djokovic was red-faced and hunched over between points, with both players forcing each other through gruelling rallies, until the Serbian took control in the second set.
A valiant Djokovic, 39, put on a dazzling performance and saved 16 break points across the match, diving at the baseline and sprinting to the net.
“I didn’t see what happened, to be honest,” he told the crowd after the fan was hit.
“I’m sure that Daniil didn’t … do it intentionally. It happened by accident. Sorry for the crowd,” he said, adding that it had been a tough match.
“I know the feeling,” Djokovic told reporters later, recounting his own disqualification from the 2020 US Open after accidentally hitting a ball at a line judge.
“It’s horrible … but it happens,” he said.
Although Djokovic was three points away from winning the match, he said victory wouldn’t have come easy and that he got “lucky” in a “very exhausting” battle.
‘Gave everything’
Earlier in the women’s draw, former world No 1 Iga Swiatek staged a comeback to advance to the last 16, while home hope Zheng Qinwen survived another three-set test.
Swiatek overcame frustration to cling on as her opponent served for the set, dominating a must-win tiebreak to beat Croatia’s Donna Vekic 4-6, 7-6 (7/0), 6-2.
“I was just angry, wanted to change something, because clearly it wasn’t working,” the 25-year-old from Poland said on court.
“Sometimes if you care a bit less because you’re … probably going to lose, it also helps you a bit,” she later told reporters.
She will face American teenager Iva Jovic for a spot in the quarter-finals.
China’s Zheng was tested by Marie Bouzkova, enduring her third three-setter of the tournament to beat the Czech player 7-6 (7/3), 4-6, 7-5.
Zheng voiced frustration in each set at movement outside the court, including as she tried to salvage the second set on Bouzkova’s serve.
But she pulled out some of her most accurate tennis to clinch the last two games and win the match when 27th-ranked Bouzkova fired long.
“I gave everything,” Zheng said, having spent more than five hours on court in the first two rounds.
“Truthfully speaking, I felt a lot of pressure today,” the 23-year-old told her home crowd, thanking fans for their support.
Zheng, the current world No 54, next plays 118th-ranked Alina Charaeva, who sent out top seed Elena Rybakina in the second round.
Wimbledon champion Linda Noskova also advanced and will face Ekaterina Alexandrova in the last 16.