The Hoops threatened to cause a major upset amid a rousing atmosphere inside Celtic Park but spurned several first-half chances to take what would have been a deserved first-half lead over Carlo Ancelotti's side.
Liel Abada failed to convert a gilt-edged chance inside 40 seconds and passed up a second opportunity to beat Madrid goalkeeper Thibaut Courtois before McGregor struck the inside of the post with a sweet strike from outside the area.
Abada's half-time replacement Daizen Maeda also fluffed a glorious close range opening at the start of the second-half and Celtic were made to pay by a clinical Los Blancos side with a two-goal blitz inside four minutes turning the game on its head.
Vinicius Junior, who scored the winner in last season's final against Liverpool, finished off a flowing counter-attack to put the visitors ahead on 56 minutes before 2018 Ballon d'Or winner Luka Modric curled home a second before Celtic had fully regrouped.
Eden Hazard - a first-half replacement for the injured striker Karim Benzema - tapped home the third with 12 minutes left to seal a comfortable win in the end for Carlo Ancelloti's star-studded side.
Celtic captain McGregor, speaking to BT Sport after the match, said: "It was really enjoyable. It's ultimately disappointing to lose three goals at home but I think there was enough in the game to show we definitely belong at this level.
"We took the game to Madrid for large parts. There was just a little 10 to 15 minute spell when we lost concentration and you can see that's why they are the holders. They have so much quality on the counter attack, and we knew that.
"We tried to limit that as much as we could. We felt at that point in the game we were on top and it was maybe just a little bit naive in terms of sticking to what we were doing."
Celtic played the same front-foot attacking style that has brought them success at domestic level and McGregor admitted he felt confident beforehand that his side could take the game to such world class opposition.
"It was a great showing from the lads and we’ve been building to that type of game," he said.
“We want to play our football against anyone so we're not just going to change our style.
“We just committed to it even more, trust ourselves and trust in the manager, and he gives us full trust to do that as well.
“We created some big chances and the difference at this level is they create one clear-cut chance and take it.
"But we’ll learn from that and the boys can be proud of themselves in terms of the level of performance."
The other match in Group F saw Shakhtar Donetsk pull off a surprise 4-1 win away at second seeds RB Leipzig with former Celtic forward Marian Shved scoring twice for the Ukrainian side.