But when the final announcement was made after 11pm in Istanbul, a brief sound of disappointment gave way to silence and the crowd quickly dissipated. Tokyo had defeated Istanbul 60-36 in the final round of secret voting.
Erdogan had argued that a successful Olympic bid for Istanbul would make it a first for a predominantly Muslim country in a city linking the continents of Europe and Asia. After the vote, he expressed disappointment. "It wasn't our fate," he said, according to Turkish news reports.
The disappointment comes at a politically sensitive time for Erdogan as he faces a series of elections. The Turkish economy is looking increasingly shaky after a decade of growth and the protests have polarised the country. The Olympic bid had already become a political issue well before the disappointment.
Turkey's minister in charge of ties with the European Union, Egemen Bagis, caused a storm last month when he suggested that anti-government protesters would be to blame if Istanbul lost the bid.
It was a charge the country's main opposition party said was an attempt to "camouflage" any possible failure.
The choice of Tokyo was received like a cold shower in Madrid, where hopes had been high that the Spanish capital would win its third consecutive Olympic bid. "A dream has fled from us," Deputy Prime Minister Soraya Saenz de Santamaria wrote on Twitter after Madrid was eliminated in the first round of a ballot by the IOC.
Socialist opposition leader Alfredo Perez Rubalcaba said he was "very sorry".
"Madrid had worked hard and well" to get the Olympics, he wrote on Twitter.
Thousands of people had gathered in the city centre, where they waved red balloons while being entertained by musicians.
When the news broke that Madrid had been ousted from the race, some people broke into tears. "This is unfair. It is a fraud," one man said, slamming the IOC as a "mafia".
It was Madrid's third consecutive bid to host the summer Olympics, after it lost to London in 2012 and Rio de Janeiro in 2016.
"Not even in its worst dreams would Madrid have expected to be eliminated already in the first round of voting," the daily El Mundo said.
The choice of Madrid to host the Olympics would have lifted spirits in a Spain hit by a six-year economic slump, where more than a quarter of the workforce is unemployed, many analysts said.
"Now at a time of crisis, it would have suited us very well to celebrate the Olympics in Spain for improvements for the country, for the jobs that would have been created, to make sports more popular," national water polo representative Lorena Miranda said.
Others said that accepting a defeat was part of the spirit of sports. "To make it until the last day and to lose is tough, but Madrid needs to feel proud. It did a magnificent job," said Eduardo Portela, president of the Spanish Association of Basketball Clubs.
- AAP