Remy Gardner has become a grand prix race winner as Enea Bastianini won the Moto2 championship by finishing fifth in Portugal.
Gardner dropped as low as fourth, where he still sat as Lap 12 of 23 at Portimao began, but made the decisive pass to get back into first position on the penultimate lap.
Marini's initial spell in the lead lasted only until Turn 3 on the opening lap, when the #10 Kalex ended up in neutral and he dropped to third.
That put the #87 Onexox TKKR Kalex back in the lead and Gardner was a second clear of Bastianini by the time they started Lap 2, before Marini reclaimed second spot on Lap 4 at Turn 1.
Sam Lowes, carrying a right wrist injury from a shocking crash a week earlier in Valencia, pushed Bastianini back to fourth just four corners later.
The British title contender's EG 0,0 Marc VDS team-mate, Augusto Fernandez, did him a favour when he too got by Bastianini, on Lap 6 at Turn 14.
Fernandez and the championship leader then became embroiled in a battle as both Marini and Lowes went past Gardner at the start of Lap 8.
The positional changes saw the gap between Bastianini and Marini drop to four points as live, and it was only two points when the #33 Italtrans Kalex was put all the way back to seventh on Lap 10.
Meanwhile, Gardner too had been shuffled back, ceding third on Lap 9 as a result of a bold Fernandez move after an unsuccessful attempt a few corners earlier.
The tide started to turn for the Australian on Lap 12, when he got back in front of Fernandez and closed up to Lowes due to the Briton running wide.
Gardner picked #22 off at the start of Lap 13, at which time the deficit to Marini was 1.3s, but that was essentially gone within two more laps.
Lowes continued to stick with them until he nearly crashed on Lap 18 at Turn 13, while Bastianini was back in a stoush with Fernandez over fifth.
Gardner had been hassling Marini for some time when he went down the inside on Lap 22 at Turn 3, then threw his green and white Kalex into Turn 5 to ensure he stayed ahead of the Italian.
Having survived that moment, the son of 1987 500cc world champion Wayne skipped seven tenths clear for the beginning of the final lap, and ultimately won by 1.6s.
Marini was second on the day and in the championship, and a gallant Lowes rounded out the podium.
Gardner ended up sixth for the season overall, but fifth in the finale was enough for Bastianini to be crowned champion with a nine-point margin.