Rio de Janeiro will host the 2016 Olympics after favourites Chicago suffered a crushing defeat.
The Brazilian city will become the first South American city to host the Games after an historic vote by the International Olympic Committee.
IOC president Jacques Rogge, who opened the envelope containing the name of the winning city, said: "All four projects are of the highest quality - thank you for your hard work, energy and commitment. But in every competition there can only be one winner."
Chicago had been the bookies' favourites to succeed London especially after US president Barack Obama flew into Copenhagen to address the IOC on Friday morning.
The Obama magic did not work this time as the American city was knocked out in the first round of voting, followed by Tokyo, leaving a head-to-head vote between Rio and Madrid.
The Spanish city had confounded expectations by getting so far but in the end Rio's excellent campaign - masterminded by Mike Lee, who was communications director for London's bid - proved enough.