Murray won 6-1 3-6 6-2 to set up a clash with old rival Novak Djokovic.
Murray upset world number 14 Shapovalov 6-1 3-6 6-2 in two hours and nine minutes to set up a last-16 match against old foe and world number one Novak Djokovic.
The old rivals have known each other since they were children and faced-off 36 times in their professional careers.
“He’s the world number one and I’ve got a metal hip,” Murray told Amazon Prime.
“I didn’t know I’d get opportunities to play matches like this again. In theory I shouldn’t have a chance in the match.
“But I’ve worked my hardest, put myself in a great position and it’s a fantastic opportunity to play against him again on a big court in a huge tournament.”
The 34-year-old Scot, who marked his return to clay for the first time in two years in Madrid with a straight-sets win against Dominic Thiem in the first round, proved too strong for Shapovalov in the deciding set.
Murray raced into an early lead, breaking Shapovalov’s serve twice on his way to taking the opening set in 36 minutos.
The British number three, behind Cameron Norrie and Dan Evans in the domestic rankings, heaped more pressure on Shapovalov’s serve in the fifth game of the second set.
But Murray squandered two break points and was broken for the first time in the match in the next game to trail 5-2 before Shapovalov served out to level at one set all after one hour and 25 minutes of play.
The deciding set went with serve until the fifth game, before Murray seized on his first break point of the set to edge ahead 4-2.
The Scot, currently 78th in the ATP rankings, held again for 5-2 and went 40-0 ahead on Shapovalov’s serve before sealing another impressive win on his third match point.
“I trained very hard the past four or five weeks and got my game in good shape,” Murray said of his performance post-match.
“My movement has been so much better than where it was at the beginning of the year and it makes a huge difference to my whole game.
“It allows me to make different decisions on the court. My movement won me that match tonight. I defended so many points.
“(Shapovalov) has so much power and I managed to defend lots of points and that won the match for me, so I was really happy with that.”
Murray reversed his decision to skip the entire clay-court season after being handed a wild card for Madrid and four weeks of training on the surface appears to have paid dividends.
Murray has not played Djokovic since the Qatar Open in January 2017, when the Briton was world number one. The Serbian won in three sets.
Djokovic commended the Scot on his determination to return to form after twice undergoing hip surgery – first in 2018 and then again in 2019 when his pain persisted.
The Serb told the ATP Tour local na rede Internet: “(Murray is) moving well, playing better and better. It’s really fantastic to see, because he’s an important player for our game.
“He’s made his mark in the history of our sport by winning multiple Slams and gold medals and (chegando) Não. 1 of the world. He’s one of the most important names that we have.
“To have him still compete is great, and to have him even play at (uma) high level as the time goes by is impressive, considering the surgery and what he has been through in the past few years. His resilience and fighting spirit is really inspiring.”
Djokovic beat France’s Gael Monfils 6-3 6-2 in his second-round match after being handed a bye in the opening round.