Miguel Oliveira delivered a wet-weather masterclass to win the Thai MotoGP on Sunday, but world champion Fabio Quartararo finished 17th to leave his lead at the top at just two points.

Portugal's KTM rider Oliveira took the chequered flag ahead of Australia's Jack Miller, with title contender Francesco Bagnaia of Italy third.

He is now just two points behind Yamaha's Quartararo in the standings with three races left.

It was a nightmare day for France's Quartararo and Spain's Aleix Espargaro -- third in the title chase -- hardly had a great time either.

He came 11th in the first MotoGP in Thailand since 2019 and is 20 points off Quartararo. The title race is too close to call.

"Every time we get a chance to ride on the wet I'm always super fast," Oliveira said after upstaging the title contenders.

The race was delayed for almost an hour because of heavy rain over the Buriram circuit and it played out in treacherous conditions.

"Very, very, very happy," Ducati's Bagnaia, the man with all the momentum, said after closing the gap once more on Quartararo.

For Miller it was a second podium finish in a row, after his victory last weekend in Japan.

"I was pushing hard there, I didn't give up on victory," Miller said.

"It's not an easy task in these conditions."

Italian rookie Marco Bezzecchi clinched his first MotoGP pole position on Saturday but could only manage a 16th placing.

In the Moto2 race, heavy rain initially cut the race down from 24 laps to 16 with commentators later likening the start and finish line to a lake.

There was no fairy tale finish for Thai hero Somkiat Chantra -- who started the race in pole position for the first time -- crashing early in the torrential conditions after notching up a two-second lead.

At lap eight, race officials brought out the red flag.

Moves to restart with a five-lap dash were delayed with a second red flag as rain bucketed down on some parts of the track.

Race officials were forced to cancel the restart and allocate half points because two-thirds of the race distance wasn't reached -- Italy's Tony Arbolino was awarded the event ahead of Czech Filip Salac and Spanish rider Aron Canet.

Italian rider Dennis Foggia, on a Honda, won the Moto3 race from pole position -- his fourth race win of the season.

MotoGP moves to Australia's Phillip Island in mid-October before heading to Malaysia and wrapping up in Valencia, Spain.