BAGHDAD - Heavy clashes erupted in Baghdad, killing almost 20 people on Monday, after powerful Shi'ite cleric Moqtada al-Sadr said he would quit politics, prompting his loyalists to storm a government palace and fight with rival groups.

As night fell, machine-gun fire and explosions rang out, with tracer fire rising into the sky above the Green Zone that houses government headquarters and foreign embassies, in the worst fighting the Iraqi capital has seen in years.

It followed a day of violence prompted by Sadr's announcement that he would withdraw from all political activity - a decision he said was in response to the failure of other Shi'ite leaders and parties to reform a corrupt and decaying governing system.

Sadr later said he was staging a hunger strike in protest against the use of weapons by all sides.

A political standoff between Sadr and Shi'ite Muslim rivals who are mostly backed by Iran has sent Iraq spiralling into yet another round of violence as it struggles to recover from decades of war, sanctions, civil strife and endemic corruption.

Since 2003, Iraqi groups have engaged in sectarian conflict and, more recently, intra-sectarian, intra-ethnic political competition.

The latest round of violence pits Sadr's supporters, which include a heavily armed militia, against Iran-aligned rival paramilitaries and the security forces.

Sadr's win in an October election, his attempts to form a government free of Iran-backed groups, and his eventual withdrawal from parliament in favour of street protests and occupying government buildings to block political activity have all featured in the build-up of tensions that finally erupted into new violence on Monday.

Security officials said some of the clashes were between Sadr's Peace Brigades fighters and members of the Iraqi security forces tasked with protecting the Green Zone, but that Iran-aligned militias were also likely to have been involved.

Reuters could not independently verify who was shooting at who on Monday night.

Sadr announced earlier in the day he was quitting politics. "I hereby announce my final withdrawal," he said on Twitter, criticising fellow Shi'ite political leaders for failing to heed his calls for reform.

Sadr loyalists who had been occupying the parliament building for weeks then charged a government headquarters in the Green Zone, once a palace of dictator Saddam Hussein, where some jumped into a swimming pool at the palace, cheering and waving flags.

Clashes then took place between young men loyal to Sadr and supporters of the pro-Iran militias, who threw stones at each other near the Green Zone, before the nighttime armed fighting.

At least 17 people were killed and scores injured, police and medical workers said.

Iraq's military declared an open-ended nationwide curfew and urged the protesters to leave the Green Zone.

Sadr, who has drawn broad support by opposing both U.S. and Iranian influence on Iraqi politics, was the biggest winner from an October election but withdrew all his lawmakers from parliament in June after he failed to form a government that excluded his rivals, mostly Tehran-backed Shi'ite parties.

Sadr has insisted on early elections and the dissolution of parliament. He says no politician who has been in power since the U.S. invasion in 2003 should hold office.

IMPASSE

Many Iraqis worry that moves by each Shi'ite camp could lead to new civil conflict.

"The (Iran) loyalists came and burned the tents of Sadrists, and attacked protesters," said Kadhim Haitham, a supporter of Sadr.

Pro-Iran groups blamed the Sadrists for the clashes and denied having shot at anyone. "It's not true - if our people had guns why would they need to throw rocks?" said one militia member, who declined to be identified by name.

Sadr has withdrawn from politics and the government in the past and has also disbanded militias loyal to him. But he retains widespread influence over state institutions and controls a paramilitary group with thousands of members.

He has often returned to political activity after similar announcements, although the current deadlock appears harder to resolve than previous periods of dysfunction and has given Iraq its longest run without a government.

Supporters of the mercurial cleric first stormed the Green Zone in July. Since then, they have occupied parliament, halting the process to choose a new president and prime minister.

Sadr's ally Mustafa al-Kadhimi, who remains caretaker prime minister, suspended cabinet meetings until further notice after protesters stormed government buildings and has publicly pleaded with Sadr to intervene to stop the violence.

Iraq, OPEC's second-largest producer, has struggled to recover since the defeat of Islamic State in 2017 because political parties have squabbled over power and its vast oil wealth.

(Reporting by John Davison in Baghdad and Amina Ismail in Erbil, Iraq; Additional reporting by Alaa Swilam; Writing by Lina Najem; Editing by Edmund Blair and Rosalba O'Brien)