Medvedev has shown his best form this week since reaching the Australian Open final in January and won the opening set 6-4 in Kazakhstan.
Djokovic hit back to win a compelling tie-break 8-6 in the second and had barely finished celebrating when Medvedev explained he would not be continuing and shook hands.
Djokovic, who won the Tel Aviv Open last week, was as shocked as the fans in the arena, saying: "I'm still surprised that he retired. He looked completely fine. The last seven or eight points I've seen him move slightly slower but it's a shock.
"I was ready for a third-set battle. I really hope his injury is nothing too serious. I know Daniil, he's a great guy, he's a fighter, he wouldn't retire if he didn't feel he couldn't continue.
"It was such a close match. I'd probably say he was the better player in the two sets. I was fighting and trying to find a way."
Medvedev later revealed he had suffered a thigh injury, saying: "It's the second time in my life that I retired like this. On the second point of the tie-break I felt a strange pop in my adductor.
"At first I thought maybe it's a cramp then after the point I was like, 'Nah, probably not'. And during the tie-break I just felt I can play five, 10 more points but that's it. If I play one more set you can miss half a year instead of one month or one month or a half."
In the final, Djokovic will take on Stefanos Tsitsipas, who battled back from a set down to defeat Andrey Rublev 4-6 6-4 6-3.
There will be an all-American final at the Japan Open in Tokyo after Taylor Fritz and Frances Tiafoe won tight tussles on Saturday.
Fritz only just made it to Japan in time for his first match after a week in Covid quarantine in Korea but he continued his progress with a 6-3 6-7 (5) 6-3 victory over Denis Shapovalov, which guaranteed he will make his top-10 debut next week.
Fritz said: "It's pretty amazing what I've been able to accomplish this week. Just from thinking that I might not even be able to play this tournament to then seven days in a room, wake up at 5am, take a flight here and just show up and play. It's pretty crazy how quick things happen. It was only four days ago."
Tiafoe has carried his excellent form into the Asian swing and overcame a major second-set blip to defeat South Korean Kwon Soonwoo 6-2 0-6 6-4.