Djokovic was already guaranteed to top the group and play Taylor Fritz in the semi-finals on Saturday but he dug deep to avoid defeat, completing a 6-3 6-7 (5) 7-6 (2) victory after three hours and 11 minutes.
The 35-year-old was clearly struggling physically late in the second set and into the third and Medvedev served for the match at 5-4 in the decider, but Djokovic found a way back.
The Serbian told Amazon Prime Video: “It’s a huge relief. 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.”
Medvedev, who won the title in 2020 and reached the final again last year, leaves Turin having lost each of his three group matches in deciding tie-breaks.
In the doubles, Britain’s Neal Skupski and Dutchman Wesley Koolhof kept their semi-final hopes alive with victory over Ivan Dodig and Austin Krajicek but they need Nikola Mektic and Mate Pavic to beat Nick Kyrgios and Thanasi Kokkinakis to make the last four.