Media playback is unsupported on your device Media caption Soldiers opened fire on police to rescue President Correa

Ecuador's President Rafael Correa has been rescued from a hospital in the capital, Quito, where he had been trapped for several hours in an uprising by disaffected police.

Widespread gunfire was heard as the army moved in to free the president, who was there for treatment after being attacked by police with tear gas.

Mr Correa went on to address supporters outside the presidential palace.

Two people died and dozens were injured in the unrest, officials said.

The president and his supporters said the police revolt over a new law cutting benefits for public servants amounted to an an attempted coup.

Mr Correa, a 47-year-old US-trained economist, took power in 2007 and was elected for a second term in 2009, despite a decision to default on $3.2bn of global bonds causing widespread fiscal problems for the government.

'Kill the president'

Mr Correa had been holed up in the police hospital, where he was treated after being hit by tear gas in a confrontation.

Hundreds of police, angry over a law that would cut their benefits, appeared to have prevented him from leaving the clinic.

Analysis The sight of two key state institutions, the national police force and the military, exchanging gunfire will be one which worries many ordinary Ecuadoreans, and reminds them of the past. Ecuador's history is peppered with violent street uprisings which often ended with the removal of the head of state. In this instance, there was to be no such outcome, but it was a sign of how polarised life in Ecuador has become in recent years, with Mr Correa dividing opinion across the country. The initial reason for the protests -austerity measures - was almost lost among the high drama of the presidential siege. But that, and other issues such as an impending decision on whether to dissolve parliament and call an early general election, are facing Mr Correa when he recovers from what was, without doubt, his toughest day since taking office. Ecuador protests: Your photos

Under cover of darkness Mr Correa was reportedly smuggled out of the hospital in a wheelchair as a gun battle between troops and police raged.

Speaking to his supporters outside the presidential palace, Mr Correa said he hoped the events of the day would serve "as an example to those who want to bring a change and stop the citizens' revolution without going through the polls".

"I give so much thanks to those heroes who accompanied me through this hard journey," the Reuters news agency reported him saying.

"Despite the danger, being surrounded, ministers and politicians came, to die if necessary. With that bravery, with that loyalty, nothing can defeat us."

The commander of Ecuador's police force has resigned, a police spokesman said on Friday.

The drama began on Thursday morning when members of the armed forces and police angry at the austerity measures occupied several barracks and set up road blocks across the country.

TV stations showed images of police setting tyres on fire in the streets of Quito, Guayaquil and other cities. The National Assembly building was also occupied.

Police also took control of Quito's international airport for several hours.

Looting was reported in the capital and Ecuador's largest city, Guayaquil. Banks were robbed and schools and businesses closed.

Peru and Colombia closed their borders with Ecuador in solidarity.

In an emotional speech to soldiers from Quito's main barracks, President Correa tore at his shirt and said: "If you want to kill the president, here he is. Kill him, if you want to. Kill him if you are brave enough."

Moments later he was forced to flee the barracks wearing a gas mask when tear gas was fired by the protesters, and he was taken to hospital.

Regional support

During the day Mr Correa received strong support from governments throughout the Americas, with a string of Latin American nations and the US all speaking up for the embattled president.

Within hours of the violence erupting in Ecuador, the South American regional body, Unasur, called an emergency meeting.

Ecuador's political instability 2005: President Lucio Gutierrez deposed after two years, following massive protests in response to his attempt to overhaul the Supreme Court

2000: Jamil Mahuad, elected 1998, forced to step down as president after two years following indigenous protests led by Col Lucio Gutierrez

1997: President Abdala Bucaram, nicknamed "El Loco" ("the crazy one"), declared mentally unfit to rule after a year in power

1987: President Leon Febres Cordero kidnapped and beaten up by the army in protest at policies of privatisation and public expenditure cuts Ecuador timeline

Mr Correa has blamed the Patriotic Society Party (PSP), led by Lucio Gutierrez, for fomenting the unrest, and said "bad elements" in the police force would "be removed".

On Wednesday, one minister had said the president was considering disbanding Congress because members of his Country Alliance had threatened to block proposals to shrink the bureaucracy.

Ecuador's two-year-old constitution allows the president to declare an impasse and rule by decree until new elections. However, such a move would have to be approved by the Constitutional Court.

The BBC's Will Grant, in Venezuela, says Mr Correa could still choose to rule by decree in an effort to stay in control in the immediate future.