President Gotabaya Rajapaksa fled Sri Lanka on Wednesday, a government official said, just days after protesters stormed his official residence as the island nation grapples with its worst economic crisis since independence.

The president, who was due to resign the same day, boarded a military transport plane with his wife and others and flew to the Maldives in the Indian Ocean, the official said, drawing to a close the lengthy Rajapaksa family rule.

Sri Lankan President Gotabaya Rajapaksa. (Getty/Kyodo)

Rajapaksa has already signed a resignation letter dated Wednesday, according to local media. The selection of a new president is planned for July 20.

Prime Minister Ranil Wickremasinghe on Wednesday declared a state of emergency across Sri Lanka as acting president, Reuters news agency reported.

Last Saturday, several thousand demonstrators entered the president's official residence in the country's largest city Colombo and seized the compound, demanding he step down.

The protesters managed to enter the residence despite police attempts to stop them by using tear gas and water cannons.

Rajapaksa tried unsuccessfully to leave for Dubai in the United Arab Emirates on Monday. His attempt was thwarted at airport immigration. There is speculation he feared losing immunity from arrest after no longer being president.

In April, Sri Lanka said it would default on its external public debt pending a bailout package from the International Monetary Fund due to critically low foreign exchange reserves.

It was the first debt default announced by the country of 22 million people since it gained independence from Britain in 1948.

The South Asian island had been saddled with heavy borrowings from China for unsustainable infrastructure projects, in what has become known as debt-trap diplomacy. Its economic crisis deepened when its cash-earning tourism sector was wracked by the coronavirus pandemic, followed by a surge in energy prices in the wake of Russia's war in Ukraine.

Sri Lankan citizens are experiencing a severe lack of fuel, which is mostly imported. Long queues have formed at gas stations in Colombo, with very few cars seen driving on well-maintained toll roads.

The Rajapaksas have ruled the South Asian country for much of the last two decades, with the president's elder brother, Mahinda Rajapaksa, serving previously as president and prime minister.

When his younger brother was elected president in 2019, Mahinda Rajapaksa became prime minister again. But he was forced to step down on May 9 after growing public discontent with the Rajapaksa family's rule amid economic difficulties.

Following the resignation, Gotabaya Rajapaksa appointed Wickremasinghe, a former prime minister, as the new prime minister.