Dalla Porta was leading in the early stages when his Leopard Honda bike suffered an engine failure, with Tony Arbolino (VNE Snipers) later also retiring from the lead with an apparent mechanical problem.
Bester Capital Dubai rider Masia meanwhile crashed at Turn 1 while being squeezed by Darryn Binder (CIP), the Spaniard retiring for the third time this season.
Binder later also retired from podium contention, having been taken out by pole-sitter Gabriel Rodrigo (Gresini) at Turn 10 on the penultimate lap.
That incident was followed by Honda Team Asia's Kaito Toba leading into the final lap, with Aron Canet and Alonso Lopez battling behind him.
Toba held the lead after the main straight but the two local riders were all over him at Turn 10 and Toba cracked under the pressure, sliding off track.
In the mayhem, Ramirez moved ahead of both Canet and Lopez, and he completed the remaining four corners of the race to score his first win of the season.
Canet finished second as Sky VR46 rookie Celestino Vietti picked off Lopez at the final turn to snatch the final podium position.
Vietti's teammate Dennis Foggia was fifth ahead of front row starter Ai Ogura (Honda Team Asia) with Romano Fenati (VNE Snipers) taking his best finish since his Moto3 return in seventh.
The top 10 was completed by Ayumu Sasaki (Petronas Sprinta), EG0,0 wildcard Ryusei Yamanaka and Pruestl's Jakub Kornfeil.
Title contender Niccolo Antonelli fell out of podium contention after running wide at Turn 1, the Italian settling for 11th, while John McPhee miraculously avoided a highside at Turn 10 of the final lap. While he managed to hang on to his bike, he dropped from the lead pack to 13th.
The race also saw a dramatic multi-bike crash caused by Can Oncu losing control of his bike at Turn 4.
He first wiped out Albert Arenas and then collected other riders, with Sergio Garcia, Raul Fernandez, Vicente Perez and Filip Salac all hitting the deck simultaneously.
Tatsuki Suzuki fell on the opening lap at Turn 10 with Andrea Migno and Makar Yurchenko also having incidents during the race.
With his second-place finish, Canet's lead is up to 23 points over Dalla Porta in the championship.
|1|| Marcos Ramirez ||Honda||21|
|2|| Arón Canet ||KTM||21||0.119|
|3|| Celestino Vietti Ramus ||KTM||21||0.146|
|4|| Alonso López ||Honda||21||0.235|
|5|| Dennis Foggia ||KTM||21||0.947|
|6|| Ai Ogura ||Honda||21||1.008|
|7|| Romano Fenati ||Honda||21||1.068|
|8|| Ayumu Sasaki ||Honda||21||1.358|
|9|| Ryusei Yamanaka ||Honda||21||1.984|
|10|| Jakub Kornfeil ||KTM||21||2.472|
|11|| Niccolo Antonelli ||Honda||21||2.729|
|12|| Carlos Tatay ||KTM||21||2.980|
|13|| John McPhee ||Honda||21||3.264|
|14|| Tom Booth-Amos ||KTM||21||11.120|
|15|| Darryn Binder ||KTM||21||18.467|
|16|| Kazuki Masaki ||KTM||21||21.845|
|17|| Riccardo Rossi ||Honda||21||39.997|
|18|| Vicente Pérez ||KTM||21||54.306|
|19|| Tatsuki Suzuki ||Honda||21||1'00.726|
| Kaito Toba ||Honda||20||1 Lap|
| Gabriel Rodrigo ||Honda||19||2 Laps|
| Makar Yurchenko ||KTM||18||3 Laps|
| Jaume Masia ||KTM||13||8 Laps|
| Tony Arbolino ||Honda||12||9 Laps|
| Filip Salač ||KTM||9||12 Laps|
| Andrea Migno ||KTM||5||16 Laps|
| Can Öncü ||KTM||5||16 Laps|
| Raúl Fernández ||KTM||4||17 Laps|
| Sergio García ||Honda||4||17 Laps|
| Albert Arenas ||KTM||4||17 Laps|
| Lorenzo Dalla Porta ||Honda||3||18 Laps|
|View full results|