LONDON, June 30 (Reuters) - Former London mayor Boris Johnson, favorite to become Britain's prime minister, abruptly pulled out of the race on Thursday, upending the contest less than a week after leading the campaign to take the country out of the EU.

Johnson's announcement, to audible gasps from a roomful of journalists and supporters, was the biggest political surprise since Prime Minister David Cameron quit on Friday, the morning after losing the referendum on British membership in the bloc.

It makes Theresa May, the interior minister who backed remaining in the European Union, the new favorite to succeed Cameron.

May, a party stalwart seen as a steady hand, announced her own candidacy earlier on Thursday, promising to deliver the withdrawal from the EU voters had demanded, despite having campaigned for the other side.

"Brexit means Brexit," she told a news conference.

"The campaign was fought, the vote was held, turnout was high and the public gave their verdict. There must be no attempts to remain inside the EU, no attempts to rejoin it through the back door and no second referendum."

Dylan Martinez / Reuters Britain's Home Secretary, Theresa May, delivers a speech at RUSI (Royal United Services Institute) in London, Britain June 30, 2016.

The decision to quit the EU has cost Britain its top credit rating, pushed the pound to its lowest level since the mid-1980s and wiped a record $3 trillion off global shares. EU leaders are scrambling to prevent further unraveling of a bloc that helped guarantee peace in post-war Europe.

The International Monetary Fund said on Thursday uncertainty over Brexit would hurt economic growth in Britain and the rest of Euorpe, and impact output globally. nL1N19M0VW

Johnson, whose backing for the Leave cause was widely seen as essential to its victory, saw his leadership bid suddenly crumble after his Brexit campaign ally, Justice Secretary Michael Gove, withdrew support and announced a bid of his own.

"I must tell you, my friends, you who have waited faithfully for the punchline of this speech, that having consulted colleagues and in view of the circumstances in parliament, I have concluded that person cannot be me," Johnson said at the close of his speech at a London luxury hotel.

The bombshell stunned supporters gathered for what they thought would be the first speech of his leadership campaign. Johnson began by hailing a "moment for hope and ambition for Britain, a time not to fight against the tide of history but to take that tide at the flood and sail on to fortune."

But by the time he spoke his bid had already been undermined by Gove, a close friend of Cameron's despite differences with the prime minister over Europe, who had previously said he would back Johnson.

In an article on Thursday in the Spectator, a magazine Johnson used to edit, Gove wrote that he had come "reluctantly, to the conclusion that Boris cannot provide the leadership or build the team for the task ahead."

Jack Taylor via Getty Images Justice Secretary and leading Brexit campaigner Michael Gove leaves his home in Kensington before announcing his intention to run to be the next Conservative Party leader and UK prime minister on June 30, 2016 in London, England.

The main opposition Labour Party also faces an acrimonious leadership battle, with lawmakers having overwhelmingly voted to withdraw confidence in left-wing party leader Jeremy Corbyn, who refuses to step down. Corbyn's party critics say his campaign to remain in the EU was half-hearted. He says he was chosen by grassroots activists and should not be pushed out.

The vacuum at the top of both major parties has added to the uncertainty at a time when Britain faces its biggest constitutional change since the dissolution of its empire in the decades after World War Two.

'DIES BY THE SWORD'

Conservative lawmakers said Johnson may have been undone by supporters of Cameron exacting revenge for his decision to defy the prime minister and back the Leave campaign.

"He who lives by the sword, dies by the sword," said one lawmaker, describing internal party conflict on condition of anonymity. The lawmaker told Reuters Johnson had realized his bid would fail after lawmakers defected from his campaign overnight.

Johnson is the latest political casualty of a civil war in the ruling party unleashed by Cameron's decision to hold the referendum on membership in the EU, an issue that divided the Conservatives for decades and now divides the country.

Known for a jokey public persona and a mop of unkempt blonde hair, Johnson became a popular national figure during eight years as London mayor, and used his charm to aid the Leave cause after deciding only late in the day to push for Brexit.

But in the week since his side won, several leading Conservatives questioned whether Johnson had the gravitas to run tough talks to mend the broken relationship with the EU.

In an article in the Times newspaper, May took aim at Johnson's persona by saying government was not "a game."

Toby Melville / Reuters Vote Leave campaign leader, Boris Johnson, waves as he finishes delivering his speech in London, Britain June 30, 2016.

Britain's new prime minister faces a huge task to unite the party and country, and persuade the EU to offer a deal balancing the desire expressed by voters to reduce immigration with London's desire to maintain access to EU markets. EU leaders say Britain must allow free movement of people to keep open access to the common market.

A new British leader will also need to reassure financial markets, which have plummeted since the referendum. The pound briefly rose after Johnson's announcement.

"The market reaction was that it makes Theresa May a shoo-in, which is less confrontational, less damaging," said Marc Ostwald, strategist at ADM Investor Services.

But some Conservative lawmakers said it was too early to crown May. Conservative members of parliament will first narrow a field of five candidates down to two, and then party members will elect the leader by Sept. 9.

POLITICAL VACUUM

In addition to May and Gove, the candidates are Stephen Crabb, the cabinet minister responsible for pensions, who campaigned to stay in the EU, and two pro-Brexit figures, Liam Fox, a right-wing former defense secretary, and Andrea Leadsom, a minister in the energy department.

Leadsom suggested the next prime minister should come from the Brexit camp. Crabb, who like May has vowed to carry out Brexit despite having opposed it, has said control over immigration should be a red line in talks with the EU.

Scottish leader Nicola Sturgeon blamed the Conservative government for recklessly holding the referendum "purely for internal leadership purposes."

Scots voted by a margin of nearly two to one to stay in the EU, and Sturgeon has said they must not be dragged out against their will, raising the prospect that the United Kingdom itself could break apart.

"Each and every one of you should be deeply, deeply ashamed of yourselves," she said to Conservative lawmakers in the Scottish parliament.

(Additional reporting by Liz O'Leary in Edinburgh, Andy Bruce, Michael Holden, Paul Sandle, Estelle Shirbon, William Schomberg and Guy Faulconbridge in London; editing by Peter Graff)