On most days in the Iraqi capital, jackhammers and electric drills provide the soundtrack to a construction boom, with multistory restaurants taking shape and a new $800 million central bank building rising above the skyline.

But this apparent prosperity in parts of Baghdad belies what many Iraqi officials and citizens see as the crumbling foundation of the state — an oil-rich Middle Eastern country that the United States had intended to be free and democratic when it led an invasion 19 years ago to topple the dictator Saddam Hussein.

After the invasion, Iraq’s long-sidelined Shiite Muslim majority came to dominate government, and the power struggle between Shiite and Sunni political groups fueled a sectarian war. Now, in a dangerous threat to the country’s already tenuous stability, rival Shiite armed groups, the most powerful among them tied to neighboring Iran, are fighting each other, and are beyond the control of the central government

Internally, externally, at the political level and at the security level, Iraq is now a failed state,” said Saad Eskander, an Iraqi historian. “The Iraqi state cannot project its authority over its territory or its people.”

Iraq’s weaknesses once again came into sharp relief last week when a stalemate over forming a new government — almost a year after the last elections — exploded into violence in the heart of the capital.

Followers of the influential Shiite cleric Muqtada al-Sadr stormed the heavily guarded Green Zone in an antigovernment protest after Mr. Sadr announced he was withdrawing from politics. Then rival pro-Iranian Shiite paramilitary fighters on the public payroll began shooting at the protesters, and armed members of a Sadr militia emerged to fight them.

Ordered by the prime minister not to shoot at the demonstrators, government security forces were largely sidelined while the rival militias fought it out. After two days of fighting killed 34 people, Mr. Sadr ordered his followers to withdraw from the Green Zone, restoring an uneasy calm.

The violence was rooted in a stalemate over forming a government that has dragged on since the elections in October 2021.

Mr. Sadr’s followers won the largest bloc of seats in Parliament, although that was not enough to form a government without coalition partners. When he failed to put together a ruling coalition, the major Iran-backed parties with paramilitary wings — Shiite political rivals to Mr. Sadr — stepped in and tried to sideline him.

Mr. Sadr then turned to his power on the street rather than at the negotiating table, ordering his followers to set up a protest camp at Parliament — a tactic he has used in the past.

“If we discuss post-2003 Iraq, then we have to say it has never actually been a functioning state,” said Maria Fantappie of the Center for Humanitarian Dialogue, a Swiss-based conflict management organization. “We never had a prime minister with total control of the security forces or the borders.”.

That Iraq has not collapsed is thanks largely to the country’s immense oil wealth. But most citizens never see the benefit of that wealth, suffering through daily electricity cuts, decrepit schools and a lack of health care or even clean water.

Last month, the country’s respected finance minister, Ali Allawi, resigned with a stark warning that staggering levels of corruption were draining Iraqi resources and posed an existential threat.

“Vast underground networks of senior officials, corrupt businessmen and politicians operate in the shadows to dominate entire sectors of the economy and siphon off literally billions of dollars from the public purse,” Mr. Allawi wrote in his resignation letter to the prime minister. “This vast octopus of corruption and deceit has reached into every sector of the country’s economy and institutions: It must be dismantled at all costs if this country is to survive.”

Mr. Allawi, who also served as finance minister in 2006, said he was shocked when he returned at “how far the machinery of government had deteriorated” under the domination of special interest groups tied to various countries in the region.

“You have the people who fly off to Tehran, fly off to Amman, fly off to Ankara, fly off to the U.A.E., fly off to Qatar,” he said in an interview with The New York Times in June. “Before, they used to fly off to Washington, but they don’t do that anymore.”