Puerto Rico Gov. Ricardo Rosselló announced late Wednesday that he will resign Aug. 2, conceding power after nearly two weeks of furious protests and political upheaval touched off by a leak of crude and insulting chat messages between him and his top advisers.

A crowd of thousands outside the governor's mansion in Old San Juan erupted into cheers and singing after Mr. Rosselló's announcement on Facebook just before midnight.

"My only priority has been the transformation of our island and the well-being of our people," a shaken-looking Mr. Rosselló said in an address that listed his accomplishments before making clear he was resigning. Addressing the protests, Mr. Rosselló said, "The demands have been overwhelming and I've received them with highest degree of humility."

The 40-year-old son of a former governor, Mr. Rosselló became the first chief executive to resign in the modern history of Puerto Rico, a U.S. territory of more than 3 million American citizens without full representation in Congress or the right to vote for president.

Justice Secretary Wanda Vázquez will assume the post less than halfway through Mr. Rosselló's four-year term, becoming Puerto Rico's second female governor.

"It's historic, but we have to be cautious. What will happen beyond this? There are concerns, but there is also hope," designer Jalil Serrano said. Gesturing to the young crowd outside the mansion, he said, "This belongs to them."

Daniel López, a businessman also in the protest, wiped tears from his eyes as people leaped into the air, beat drums, waved flags, hugged, and cried, "We did it!"

"This is for the future of my family," Mr. López said. "It's big, what's happened."

Mr. Rosselló's announcement came after a bizarre, hours-long standoff unfolded in colonial Old San Juan, as the governor pledged to deliver a message to the people of Puerto Rico, then passed hour after hour in unexplained silence while thousands of protesters chanted demands for his resignation.

An announcement was first expected at 5 p.m., then finally came less than a half-hour before midnight.

"Despite expecting to service the term that the people democratically elected me to, today I feel that continuing in this position represents a threat to the success we have achieved," Mr. Rosselló said.

Puerto Rico Rep. Gabriel Rodríguez, a member of Mr. Rosselló's pro-statehood party, told The Associated Press that legislators had initially agreed to set aside the impeachment process and give the governor until 5 p.m. to announce that he was going to resign. The president of Puerto Rico's House of Representatives issued the embattled governor an ultimatum: Either take the best decision for a U.S. territory demanding his resignation or face an impeachment process.

At one point, dozens of officers in full riot gear marched out of the governor's mansion toward protesters.

"We want peace, and they want war!" the crowd yelled as many became increasingly frustrated.

Hundreds of thousands of Puerto Ricans have been outraged by the online chats between Mr. Rosselló and his advisers, and have protested for nearly two weeks demanding his resignation.

The chat participants discussed the awarding of government contracts in ways that some observers called potentially illegal. They also insulted women and mocked constituents, including victims of Hurricane Maria.

On Tuesday, officials announced that a Puerto Rico judge had issued search warrants for the cellphones of government officials involved in the chat as part of an investigation. One of the search warrants said officials used the chat to transmit official and confidential information to private citizens in potential violation of ethics laws.

More than a dozen government officials have resigned since the chat was leaked earlier this month, including Mr. Rosselló confidant and chief of staff Ricardo Llerandi, former Secretary of State Luis Rivera Marín, and former chief financial officer Christian Sobrino, who also held five other positions.

The obscenity-laced online messages involving the governor and 11 other men infuriated Puerto Ricans already frustrated with corruption, mismanagement, economic crisis, and the sluggish recovery from Hurricane Maria nearly two years ago.

In reaction, tens of thousands took to the streets to demand Mr. Rosselló's resignation in Puerto Rico's biggest demonstrations since the protests that put an end to U.S. Navy training on the island of Vieques more than 15 years ago.

Under Puerto Rico's constitution, the secretary of state would normally assume the governorship, but since Mr. Marín became one of more than a dozen officials to resign in the uproar over the leak, leadership of the island falls to Ms. Vázquez.

Over the weekend, Mr. Rosselló posted a video on Facebook in which he announced he would not seek re-election in 2020 or continue as head of his pro-statehood political party, but his refusal to resign further angered Puerto Ricans and led to a colossal demonstration Monday on one of the capital's main highways.

Pressure on Mr. Rosselló to step down included calls from Puerto Rico music stars Ricky Martin, Bad Bunny, and Residente and a string of U.S. politicians, including members of Congress from both parties and several Democratic presidential candidates.

The upheaval comes as the island tries to restructure part of $70 billion in debt and cope with a 13-year recession that has led to an exodus of nearly half a million people to the U.S. mainland in the past decade. Many Puerto Ricans are resentful over the resulting pension cuts, school closings, and other austerity measures.

The economic crisis is in part a result of previous administrations – including that of Mr. Rosselló's father, Pedro – that overspent, overestimated revenue, and borrowed millions as the island sank deeper into debt. In 2017, Puerto Rico filed for the equivalent of bankruptcy. Congress approved a financial package, and a federal board is overseeing the island's finances.

An MIT graduate with a doctorate in genetics, Mr. Rosselló spent much of his time as governor fighting austerity measures and seeking federal funds after Maria devastated the island in September 2017, causing thousands of deaths and more than $100 billion in damage.

Nearly two years later, some 30,000 homes still have tarp roofs, power outages remain common, and Puerto Rico has received less than a third of the roughly $40 billion pledged by the U.S. government. Mr. Rosselló complained earlier this year of unfair treatment and a hostile attitude from some U.S. officials.

Get the Monitor Stories you care about delivered to your inbox. By signing up, you agree to our Privacy Policy

The public's confidence has also been rocked by a recent string of corruption arrests involving such figures as the island's former education secretary and the one-time chief of health services.

This story was reported by The Associated Press. Associated Press writers Mariela Santos in San Juan and Michael Weissenstein in Havana contributed to this report.