With overtaking impossible, the wet-to-dry race was all but decided when the leaders timed their final pitstop from to slicks.
That transition caught out Ferrari, with polesitter and erstwhile leader Charles Leclerc fuming as poorly timed stops from wets to inters and then to slicks saw him slip to fourth behind Sainz and both Red Bulls.
Sainz was still in with a chance to win the race, however, having pitted from the lead on lap 21 to go straight to slicks.
That would have given him a chance to stay ahead of Red Bull's Perez, who had to come in again for hard tyres one lap later on the largely dry track.
But Perez still came out two second ahead as Sainz encountered traffic on his outlap, which the Spaniard felt cost him the lead and eventual victory.
"I felt like we did everything that we had to do out there," Sainz said. "We stayed patient on the wets, we took the right decision to go on to the slick and a terrible outlap there stuck behind a lapped car cost me a race win today.
"You can understand the frustration because a clean out lap would have secured me the race win today. But it's how this sport is sometimes."
While most teams decided on a stint on intermediates before going onto slicks, Sainz overruled his Ferrari team and decided to go straight from the extreme wet tyres onto the slicks.
Sainz said he realised early on in the first stint that skipping the intermediates phase would give him the best chance to come out ahead.
"Yeah, I knew it from halfway through the first stint. I started to see the dry line and I started to realise that it was going to go straight into slicks," Sainz explained.
"I think we did the right call as we were leading the race, basically. Then we pitted for that hard tyre. Obviously hard is never easy on the outlap, but I had to do 12 corners or something like that behind the lapped car that cost me at least a couple of seconds that cost me a race win.
"Anyway, I'm not gonna complain too much. I know that this sport is like that. Checo was unlucky in Jeddah, today he did a great race plus got lucky with myself and in this sport, it will turn around one day or later."
After the pitstops, Perez lead Sainz and Max Verstappen home as the clock hit zero on a race that was first red flagged for heavy rain, and then a second time for a heavy shunt for Haas driver Mick Schumacher.
A frustrated Leclerc came home fourth, with another home victory in Monaco eluding him when he had the car to win. Leclerc also lost a further three points to Verstappen in the drivers' championship, now trailing the Dutchman by nine points.