Gabriel Medina fought off Kelly Slater and Mick Fanning on the final day of the Pipe Masters to be crowned the first Brazilian ASP world champion.