Charles Leclerc was on course to finish third until Australian Oscar Piastri of McLaren dramatically passed him on the final lap.