COLOMBO, Sri Lanka — Sri Lanka’s president and prime minister agreed to resign Saturday after the country’s most chaotic day in months of political turmoil, with protesters storming both officials’ homes and setting fire to one of the buildings in a rage over the nation’s severe economic crisis.

Prime Minister Ranil Wickremesinghe said he will leave office once a new government is in place, and hours later the speaker of Parliament said President Gotabaya Rajapaksa would step down Wednesday.

Pressure on both men grew as the economic meltdown set off acute shortages of essential items, leaving people struggling to buy food, fuel and other necessities.

Police had attempted to thwart promised protests with a curfew, then lifted it as lawyers and opposition politicians denounced it as illegal. Thousands of protesters entered Colombo and swarmed into Rajapaksa’s fortified residence. Video images showed crowds splashing in the garden pool, lying on beds and using their cellphone cameras to capture the moment.

“I came here today to send the president home,” said Wasantha Kiruwaththuduwa, 50, who had walked 10 miles to join the protest. “Now the president must resign. If he wants peace to prevail, he must step down.”

It was not clear if Rajapaksa was there at the time, and government spokesman Mohan Samaranayake said he had no information about the president’s movements.

Protesters later broke into the prime minister’s private residence and set it on fire, Wickremesinghe’s office said. It wasn’t immediately clear if he was there when the incursion happened.

Earlier police fired tear gas at protesters who gathered in the streets to march on the presidential residence, waving flags, banging drums and chanting slogans. In all, more than 30 people were hurt in Saturday’s chaos.

Speaker Mahinda Yapa Abeywardena said in a televised statement that he informed Rajapaksa that parliamentary leaders had met and decided to request he leave office, and the president agreed. However Rajapaksa will remain temporarily to ensure a smooth transfer of power, Abeywardena added.

“He asked me to inform the country that he will make his resignation on Wednesday the 13th, because there is a need to hand over power peacefully,” Abeywardena said.

“Therefore there is no need for further disturbances in the country, and I urge everyone for the sake of the country to maintain peace to enable a smooth transition,” the speaker continued.

Opposition lawmaker Rauff Hakeem said a consensus was reached for the speaker of Parliament to take over as temporary president and work on an interim government.

Wickremesinghe announced his own impending resignation but said he would not step down until a new government is formed, angering protesters who demanded his immediate departure.

“Today in this country we have a fuel crisis, a food shortage, we have the head of the World Food Program coming here and we have several matters to discuss with the IMF,” Wickremesinghe said. “Therefore, if this government leaves there should be another government.”

Wickremesinghe said he suggested an all-party government to the president, but didn’t say anything about Rajapaksa’s whereabouts. Opposition parties were discussing the formation of a new government.

Rajapaksa appointed Wickremesinghe as prime minister in May in the hope that the career politician would use his diplomacy and contacts to resuscitate a collapsed economy. But people’s patience wore thin as shortages of fuel, medicine and cooking gas only increased and oil reserves ran dry. Authorities have also temporarily shuttered schools.

The country is relying on aid from India and other nations as leaders try to negotiate a bailout with the International Monetary Fund. Wickremesinghe said recently that negotiations with the IMF were complex because Sri Lanka was now a bankrupt state.

Sri Lanka announced in April that it was suspending repayment of foreign loans due to a foreign currency shortage. Its total foreign debt is $51 billion, of which it must repay $28 billion by the end of 2027.

Months of demonstrations have all but dismantled the Rajapaksa political dynasty, which has ruled Sri Lanka for most of the past two decades but is accused by protesters of mismanagement and corruption. The president’s older brother resigned as prime minister in May after violent protests saw him seek safety at a naval base.

At least 34 people, including two police officers, were injured Saturday. Two were in critical condition, according to an official at the Colombo National Hospital who spoke on condition of anonymity.

Privately owned Sirasa Television said at least six of its workers, including four reporters, were hospitalized after being beaten by police while covering the protest.

The New York Times contributed.