Sebastian Vettel has extended his world championship lead with victory in the Hungarian GP ahead of Kimi Raikkonen and Valtteri Bottas.
Vettel, handicapped for most of the race by a steering issue, was indebted to team-mate Raikkonen not challenging him for the lead as his pace evaporated.
In a highly risky but sporting move, Lewis Hamilton surrendered third place to Mercedes stable-mate Bottas at the final corner after previously being let through by the Finn in order to attack the Ferraris.
The result means Vettel, who had started the race from pole position, will take a 14-point lead over Hamilton into the summer break at the head of the Drivers' Championship.
With internal politics rather than race pace ultimately deciding Ferrari's finishing order, Mercedes' decision to swap their drivers on the last lap will be fiercely debated - especially if it ultimately costs Hamilton a fourth world title.
"We could lose a championship because of those three points," team boss Toto Wolff told Sky Sports F1. "But this spirit has made us win three championships and it will make us win more."
With a radio malfunction preventing Hamilton from being able to communicate with his team immediately after the pit-stops, Hamilton had already lost plenty of tyre life before being allowed through by Bottas.
As Vettel's pace disappeared, and with an exasperated Raikkonen unable to persuade the Ferrari pitwall to allow him to overtake the stricken lead car, Hamilton immediately produced enough pace to catch the Ferrari train but not enough to overtake rear-gunner Raikkonen around a Hungaroring circuit where overtaking is notoriously difficult.
"Something went wrong," said a relieved Vettel. "The steering started to go sideways. Towards the end it came back when I had a cushion but I had to stay focused the whole race."
When Hamilton eventually launched his attack on Raikkonen in earnest, he understeered in the dirty air being churned out by the Ferrari and nearly ran off the track at Turn Five.
Hamilton then slowed down by as much as five seconds on the final lap to let Bottas back through and crossed the line fractionally ahead of Red Bull's Max Verstappen.
"It's tough in the championship but I'm a man of my word," Hamilton told Sky F1. "I did say that if I can't overtake them I would let him back through.
"I was nervous of losing a place to Verstappen but fortunately I didn't."
Mercedes and Hamilton defend race swap
Verstappen had earlier been embroiled in the race's most controversial incident when he crashed into team-mate Daniel Ricciardo at the second corner.
A furious Ricciardo later described Verstappen's move as "amateur" and a "sore loser".
The Budapest stewards were equally severe, meting out a 10-second penalty against the teenager. But for that transgression, Verstappen, who issued a public apology to Ricciardo, could have been an unlikely challenger for victory.
The first 30 laps of the Grand Prix were otherwise uneventful with the Red Bulls' collision triggering the only change in position for a points-paying place before the first round of pit-stops.
Fernando Alonso finished sixth, setting the race's fastest lap in the process and pulling off one of the afternoon's rare overtakes when he rounded the Toro Rosso of Carlos Sainz, to propel McLaren off the bottom of the Constructors' Championship. Team-mate Stoffel Vandoorne took 10th, behind the two Force Indias of Esteban Ocon and Sergio Perez.
Demonstrating a sense of humour to match his speed, Alonso then started his summer holidays in style with a mock salute of his deckchair sit-down in Brazil two years ago.
But Sunday's result was no laughing matter for Renault with neither Nico Hulkenberg nor Jolyon Palmer able to finish in the points in a race that had been deemed "critical" to their ambition of finishing fifth in the teams' championship. The failure adds extra significance to next week's test at the Hungaroring when Robert Kubica will be let loose in the 2017 Renault as he bids to make a remarkable F1 comeback.
Sky F1's Paul di Resta, standing in for the unwell Felipe Massa at Williams, retired from the race with 10 laps remaining.
If you are using skysports.com you can comment below to get involved in the debate, but please adhere to our House Rules. If you wish to report any comment, simply click on the down arrow next to the offending comment and click 'Report'.