Mahrez curled home a free kick in the 71st minute to give City the lead for the second time at Goodison Park, just as Everton looked like taking points off the champions to boost Liverpool, its neighbor from Merseyside.
Sterling then powered in a close-range shot off the crossbar to clinch victory for the visitors in the 84th. Gabriel Jesus, picked to start ahead of Sergio Aguero, had put City ahead in the 24th with a diving header from a curling cross from Kevin De Bruyne, who now has eight assists for the season.
Everton equalized in the 33rd when Dominic Calvert-Lewin applied the finishing touch on the goal line even though Seamus Coleman's chipped finish over City goalkeeper Ederson Moraes was going in anyway.
"Coming here is always one of the most difficult fixtures in a season," said Sterling, who scored his 100th goal for City. "The pitch is narrow and they are always up for it. "It is early in the season and there are still 90 points to play for. We are behind at this moment in time but the league can change very quickly."
After several days in the headlines following a controversial tweet by Bernardo Silva , City's players — including the Portuguese midfielder — warmed up in T-shirts supporting anti-discriminatory body Kick It Out. This was part of an initiative by Everton, who unfurled a banner in support of its forward Moise Kean, a player who suffered racial abuse while at Juventus last season.
Bernardo Silva began on the bench for City. Everton winger Theo Walcott was handed his first start of the season but his involvement lasted little more than a minute as he suffered a head injury blocking a cross from Sterling. He fell to the ground immediately and left the field on a stretcher before heading to hospital for assessment.
Everton said after the game that Walcott had been discharged and would follow concussion protocols.
More AP soccer: https://apnews.com/apf-Soccer and https://twitter.com/AP_Sports