Spanish rider Raúl Fernández won the Valencia Grand Prix while Marc Márquez finished second in his final MotoGP race.