Francesco Bagnaia beat his Ducati teammate Marc Marquez by 0.012 seconds with a 1:29.634 lap to take his first pole since Malaysia.