Dani Pedrosa claimed his first MotoGP victory since Estoril by winning a thrilling German Grand Prix at the Sachsenring.