The announcement, following the dramatic escalation in months of largely peaceful anti-government protests over a dire economic crisis on the Indian Ocean island of 22 million people, triggered an eruption of celebratory fireworks in the city.
There was no immediate word from the president himself.
Speaker Mahinda Yapa Abeywardena said in a video statement that Rajapaksa had informed him that he would step down from his post on Wednesday.
"The decision to step down on 13 July was taken to ensure a peaceful handover of power," Abeywardena said. "I therefore request the public to respect the law and maintain peace," he said.
Prime Minister Ranil Wickremesinghe also said he was willing to resign to make way for an all-party government, his office said in a statement on Saturday evening.
It was not yet clear if this would quell popular anger.
Details of how a transition of power would take place were also not yet known, although the speaker earlier outlined proposals from a meeting of political parties on Saturday that would include parliament picking an acting president within a week.
Official residence overrun
Throughout the day, soldiers and police were unable to hold back a crowd of chanting protesters demanding Rajapaksa's resignation and blaming him for the country's worst economic crisis in seven decades.
Police fired shots in the air but were unable to stop the crowd from surrounding the presidential residence, a witness said.
Neither Rajapaksa nor Wickremesinghe were in their residences when the buildings were attacked.
Inside the president's whitewashed colonial-era residence, a Facebook livestream showed hundreds of protesters, some draped in the national flag, packing into rooms and corridors.
Video footage showed some of them splashing in the swimming pool, while others sat on a four-poster bed and sofas. Some could be seen emptying out a chest of drawers in images that were widely circulated on social media.
Rajapaksa had left on Friday as a safety precaution ahead of the planned weekend demonstration, two defence ministry sources said. Reuters could not immediately confirm his whereabouts.
Later on Saturday, video footage on local news channels showed a huge fire and smoke coming from Wickremesinghe's private home in an affluent Colombo neighbourhood. His office said that protesters had started the fire.
There were no immediate reports of injuries in the blaze. Wickremesinghe had moved to a secure location, a government source told Reuters early in the day.
At least 39 people, including two police officers, were injured and hospitalised during the protests, hospital sources told Reuters.
The country is struggling under a severe foreign exchange shortage that has limited essential imports of fuel, food and medicine, plunging it into the worst economic crisis since independence in 1948.
Soaring inflation, which reached a record 54.6 percent in June and is expected to hit 70 percent in the coming months, has heaped hardship on the population.
Party talks
The decision by the president and prime minister to step aside came after Wickremesinghe held talks with several political party leaders to decide what steps to take following the unrest.
"Wickremesinghe has told the party leaders that he is willing to resign as Prime Minister and make way for an all-party government to take over," his office said in a statement.
The parliamentary speaker, Abeywardena, said in a letter to Rajapaksa that several decisions had been made at the meeting of party leaders, including the president and the prime minister resigning as soon as possible and parliament being called within seven days to select an acting president.
"Under the acting president the present parliament can appoint a new prime minister and an interim government," said the letter released by the Speaker's office.
"Afterwards under a set time an election can be held for the people to elect a new parliament," it added.
Political analyst Kusal Perera said the situation was "dicey."
"If a clear transition is not put in place the president and prime minister's resignation will create a power vacuum that could be dangerous," Perera said. "The Speaker can appoint a new all-party government but whether they will be accepted by the protesters remains to be seen."
The U.S. State Department is following developments closely and condemns any violence against peaceful protesters and journalists, a State Department spokesperson said, adding that Washington calls for the full investigation, arrest and prosecution of anyone involved in violent incidents related to protests.
"We urge this government or any new, constitutionally-selected government to work quickly to identify and implement solutions that will achieve long-term economic stability and address the Sri Lankan people’s discontent over the worsening economic conditions, including power, food, and fuel shortages," the spokesperson said.
Economic crisis
Political instability could undermine Sri Lanka's talks with the International Monetary Fund as it seeks a $3 billion bailout, the restructuring of some foreign debt and fund-raising from multilateral and bilateral sources to ease the dollar drought.
The economic crisis developed after the Covid-19 pandemic hammered the tourism-reliant economy and slashed remittances from overseas workers.
It has been compounded by the build-up of hefty government debt, rising oil prices and a ban on the import of chemical fertilisers last year that devastated agriculture. The fertiliser ban was reversed in November.
However, many blame the country's decline on economic mismanagement by Rajapaksa.
Discontent has increased in recent weeks as the cash-strapped country stopped receiving fuel shipments, forcing school closures and rationing of petrol and diesel for essential services.