Kevin De Bruyne kept his cool to beat Belgium team mate Thibaut Courtois from the penalty spot in the 82nd minute to give City the lead after Gabriel Jesus had headed home a cross from De Bruyne to pull the visitors level in the 78th minute.
Isco had given Real the lead on the hour mark following a sweeping counterattack but the 13-times European champions' night soon unravelled, culminating in Ramos being sent off for hauling down Jesus in a bid to prevent a third City goal.
Ramos will be suspended for the second leg at the Etihad Stadium on March 17 while City's influential defender Aymeric Laporte was forced off injured in the first half, another setback in a season marked by fitness troubles.
City went into the match with a two-year ban from European competition hanging around their necks for defying UEFA's rules on financial fair play although the club submitted an appeal against the sanction earlier this week.
The visiting supporters chanted against Europe's organising body throughout the game and after they had completed their comeback the sanction had become a badge of pride for the gleeful fans.
Yet he turned to Sterling in the second half and the England forward earned the penalty from which De Bruyne snatched the winner, drawing a foul from Real's Dani Carvajal.
Brazil striker Jesus led City's line from the start and had their two best chances in the first half, first drawing a save from Madrid keeper Courtois then seeing a shot hurriedly scuffed off the line by three Real players right before halftime.
Real's best chance of the first half came when Karim Benzema had a header saved by Ederson and the ball fell to Vinicius Jr. but the teenager fell over just as he was shaping to shoot into the unguarded net.
The Brazilian was not fazed by the miss, though, and played a crucial part in breaking the deadlock by chasing down Fernandinho and laying on the pass for Isco to score.
Real's fans felt they were on the familiar path to victory in their favourite competition and began to sing 'We are the Kings of Europe', but it was City who had the last laugh.