Marc Marquez has pulled off the perfect last lap scrap against Danilo Petrucci to win in the wet at Misano.