International Olympic Committee president Jacques Rogge made the announcement after London earned a majority over Paris in the fourth and final round of balloting in a meeting among members in Singapore.
The London delegates in the convention hall leaped out of their seats, arms raised in jubilation and cheering wildly after Rogge called out the city's name.
Crowds in London's Trafalgar Square went wild, shouting and waving flags in celebration.
Confetti comes down on crowds in London's Trafalgar Square celebrating the announcement by the International Olympic Committee that London won the bid to host the 2012 Olympic Games. (AP Photo/ Max Nash)
As predicted by Rogge earlier in the week, it came down to the wire. London beat Paris by four votes, 54 to 50, in the final round.
"It's just the most fantastic opportunity to do everything we ever dreamed of in British sport. This was the most splendid team performance," bid leader and former twice Olympic champion Sebastian Coe said on Wednesday.
"We are taking home the biggest prize in sport."
"This is a truly fantastic day for east and southeast London," said Robin Wales, mayor of the east London borough of Newham, where much of the Olympics will be held. "It is a massive opportunity and also a big responsibility."
Still, it was a surprising announcement.
London was considered a serious contender for the 2012 Games, but many Olympic observers didn't expect the city to upset Paris – the longtime front-runner – for the honour.
"We are very, very disappointed but it was a very good competition," said Paris bid leader Philippe Baudillon.
"We thought we could win but obviously we did not. Ah well, that's life."
The race eventually came down to a choice between Paris or London after Moscow was eliminated in the first round and then New York and Madrid in subsequent ballots.
This is the third time London will be hosting the world's biggest sporting event, but the first since 1948.
The 2012 Summer Games follow the 2010 Winter Games in Vancouver and Whistler, B.C.
"From one Olympic city to another, we salute London. The years of hard work, dedication and planning on behalf of the London bid team have resulted in a fantastic choice for the 2012 Summer Games," said John Furlong, CEO of the Vancouver organizing committee, in a statement.
"London is one of the world's great cities and will be a spectacular host for the Olympic and Paralympic Summer Games."
It was the fourth Olympic bid from Britain after failed attempts by Birmingham for the 1992 Olympics and Manchester in 1996 and 2000.
One of the most attractive components to its bid was the revitalization and redevelopment of London's east end. In the lead-up to the final vote, Prime Minister Tony Blair said the Olympics would leave a lasting legacy on the city and the country.
Unlike Paris, London doesn't have many Olympic-ready facilities.
One of the strongest components of Paris' bid was most of the infrastructure and sporting venues is already in place. Its main venue – the Stade de France – is already built and hosted the 1998 World Cup of Soccer.
Aiming for the Olympics for the third time in 20 years, Paris was considered one of the safest and best bets heading into Wednesday's voting.
In its evaluation report, the IOC gave Paris glowing reviews in following a blueprint to deliver the Games at a reduced size and cost.
In Paris, the announcement was met by stunned silence. A crowd of thousands gathered in front of City Hall in Paris for an expected celebration but ended up just quietly watching scenes of joy in the British capital.
NBA star Tony Parker couldn't hide his disappointment.
The San Antonio Spurs guard said the vote proved the IOC has an Anglo-Saxon bias.
"It's been three times now that Paris has been refused – 1992, 2008 and 2012. I find that bizarre," said Parker. "We did everything we had to do. I don't know what more we could have done.
"It proves that the committee is Anglo-Saxon. They prefer the English."
French officials struggled to comprehend how it could lose after being in a position of strength.
"I am not disappointed personally, I am disappointed for the millions of people involved in sports education in France who were the target of our bid," said Henri Serandour, president of the French Olympic Committee.

