Djokovic had already secured top spot in the Red Group but had to battle to earn a 6-3 6-7 (5-7) 7-6 (7-2) victory in three hours 11 minutes.
Medvedev led 5-4 and was serving for the match in the decider before Djokovic recovered to take the set to a tie-break.
"It's a huge relief," Djokovic told Amazon Prime Video.
"Daniil and I had some battles in the past and I knew coming into the match today it's going to be his last match of the season, and he is definitely not going to want to finish the season with a loss.
"I started off very well, had my chances early in the second. He just played very well in those decisive moments. I didn't feel physically so well in the third set to be honest, I struggled a lot staying physically there.
"I'm really proud of being able to find that last drop of energy and necessary focus in order to come back into the match.
"This match against one of the greatest rivals I have today was very important, regardless of the fact I'd qualified already and I really wanted to win."
Djokovic will face American Taylor Fritz in the semi-finals on Saturday, while Medvedev goes out having lost all three group matches.
Russia's Andrey Rublev completes the last-four line-up in the men's singles after coming from behind to beat Greece's Stefanos Tsitsipas 3-6 6-3 6-2.
Rublev qualifies for the semi-final as the Red Group runner-up and will face Norway's Casper Ruud.
Meanwhile, Britain's Neal Skupski qualified for the men's doubles final alongside Dutch partner Wesley Koolhof after results elsewhere.
Green Group winners Nikola Mektic and Mate Pavic maintained their winning run to beat Nick Kyrgios and Thanasi Kokkinakis 7-6 (7-4) 7-6 (7-4).
The result meant Skupski and Koolhof, who beat Ivan Dodig and Austin Krajicek 7-5 4-6 10-6 earlier on Friday, will face Britain's Joe Salisbury and American Rajeev Ram in the semi-finals.