Frankie Dettori and Diabolical had to settle for second in the Breeders' Cup Turf Sprint.
The Godolphin team looked booked for glory as the five-year-old burst clear approaching the final furlong.
However outsider Desert Code was to thunder from the pack and cut him down in the shadows of the post.
Fleeting Spirit looked a threat down the inside but eventually had to settle for fifth place.
Winning rider Richard Migliore said after the half-length success: "I'm so happy I can't even express how happy I am.
"I've had a lot of faith in this horse and also the trainer and the horse's work rider.
"I kind of anticipated being a bit closer to the pace, but it seemed they were flying and my horse wasn't comfortable so I just sat on him and hoped things would turn out right."
Desert Code's trainer David Hofmans added: "He loves this downhill course, he's won four now on it. "He was so far back today, this was a different style for him. I give Richard all the credit for this. I got him to the race, Richard took it from there."