A late rally allowed him to work his way down to a 1m44.485s, over one second clear of the field, although the soaked surface was far from conducive to chasing outright times.
Dani Pedrosa had been fastest with two minutes to run, but was in the pits and therefore unable to respond as Hayden made his charge.
The Honda rider, twice a winner at the Spanish track, had to settle for second, 1.005s down.
Newly crowned double-champion Jorge Lorenzo was four tenths down the road in third, ahead of Tech 3's Andrea Dovizioso.
Valencia will mark the Italian's final race with the satellite Yamaha squad before he moves across to Ducati.
Stablemate Cal Crutchlow had held the intra-team advantage when he moved into third with nine minutes to run, but with a flurry of late improvements found himself pushed down to eighth at the flag.
Separating him and Dovizioso were Gresini's Alvaro Bautista, CRT points leader Aleix Espargaro and the second Ducati of Valentino Rossi.
Rossi, like Dovizioso facing his last race for his current team, was just over two seconds down on Hayden.
The second CRT Aspar of Randy de Puniet beat Phillip Island race winner Casey Stoner to ninth.
Despite the conditions there were only two fallers - James Ellison and Katsuyuki Nakasuga, who is replacing the injured Ben Spies this weekend. Both were unhurt.
- Sports & Recreation
- Andrea Dovizioso
- Dani Pedrosa