A rejuvenated al-Qaeda-affiliated force asserted control over the western Iraqi city of Fallujah on Friday, raising its flag over government buildings and declaring an Islamic state in one of the most crucial areas that U.S. troops fought to pacify before withdrawing from Iraq two years ago.

The capture of Fallujah came amid an explosion of violence across the western desert province of Anbar in which local tribes, Iraqi security forces and al-Qaeda-affiliated militants have been fighting one another for days in a confusingly chaotic three-way war.

Elsewhere in the province, local tribal militias claimed they were gaining ground against the al-Qaeda militants who surged into urban areas from their desert strongholds this week after clashes erupted between local residents and the Iraqi security forces.

In Fallujah, where Marines fought the bloodiest battle of the Iraq war in 2004, the militants appeared to have the upper hand, underscoring the extent to which the Iraqi security forces have struggled to sustain the gains made by U.S. troops before they withdrew in December 2011.

The upheaval also affirmed the soaring capabilities of the Islamic State of Iraq and Syria (ISIS), the rebranded version of the al-Qaeda in Iraq organization that was formed a decade ago to confront U.S. troops and expanded into Syria last year while escalating its activities in Iraq. Roughly a third of the 4,486 U.S. troops killed in Iraq died in Anbar trying to defeat al-Qaeda in Iraq, nearly 100 of them in the November 2004 battle for control of Fallujah, the site of America’s bloodiest confrontation since the Vietnam War.

Events Friday suggested the fight may have been in vain.

“At the moment, there is no presence of the Iraqi state in Fallujah,” said a local journalist who asked not to be named because he fears for his safety. “The police and the army have abandoned the city, al-Qaeda has taken down all the Iraqi flags and burned them, and it has raised its own flag on all the buildings.”

At Friday prayers , held outdoors and attended by thousands of people, a masked ISIS fighter took the podium and addressed the crowd, declaring the establishment of an “Islamic emirate” in Fallujah and promising to help residents fight the government of Prime Minister Nouri al-Maliki and his Iranian allies.

“We don’t want to hurt you. We don’t want to take any of your possessions,” the man told the crowd, according to the journalist, who attended the prayers. “We want you to reopen the schools and institutions and return to your normal lives.”

The extent of the militants’ control over the city was unclear, however. Some local tribes were challenging their presence, and there were scattered firefights, according to another Fallujah resident who also did not want to be named because he is afraid. The Iraqi army fired shells into Fallujah from bases outside the city, killing at least 17 people, and most residents spent the day hiding indoors, he said.

In the provincial capital, Ramadi, tribal fighters have succeeded in ejecting al-Qaeda loyalists, according to Ahmed Abu Risha, a tribal leader who fought alongside U.S. troops against al-Qaeda in Iraq following the “surge” of U.S. troops in 2007.

The tribesmen are cooperating with Iraqi police, Abu Risha said, and are receiving weapons and support from the Iraqi army. Among those killed in the fighting was Abu Abdul Rahman al-Baghdadi, the emir, or leader, of ISIS in Ramadi.

“All the tribes of Anbar are fighting against al-Qaeda,” he said. “We are happy this fight is taking place. We will confront them face to face, and we will win this battle.”

But it was unclear whether all the tribal fighters battling the al-Qaeda-affiliated militants were doing so in alliance with the Iraqi government. The current violence evolved from a year-long, largely peaceful Sunni revolt against Maliki’s Shiite-dominated government that drew inspiration from the Arab Spring demonstrations elsewhere in the region. But it was rooted in the sectarian disputes left unresolved when U.S. troops withdrew and inflamed by the escalating conflict in neighboring Syria.

Those disputes include the exclusion of Sunnis from important decision-making positions in government and abuses committed against Sunnis in Iraq’s notoriously inequitable judicial system.

When Maliki dispatched the Iraqi army to quell a protest in Ramadi this week, local tribes fought back. Maliki ordered the troops to withdraw, creating an opportunity for al-Qaeda fighters to surge into towns from their desert strongholds and triggering battles across the province.

Though some tribes have turned against the al-Qaeda-affiliated militants, others have not, said Kirk Sowell, a political risk analyst based in the Jordanian capital, Amman, who edits the newsletter Inside Iraqi Politics.

“Basically, no one is in control,” he said. “The situation was really horrible anyway, and the operation against Ramadi made it worse.”

A group representing the tribal fighters, calling itself the Military Council of the Anbar Rebels, posted a video on YouTube in which masked men declared their opposition to Maliki’s government but made no mention of al-Qaeda. The fighters called on local members of the Iraqi security forces to desert, hand over their weapons “and remember always that they are the sons of Iraq, not slaves of Maliki.”

Whether or how the Iraqi security forces will be able to regain the initiative is unclear. ISIS fighters have steadily asserted their control over the province’s desert regions for months, buoyed by their consolidation of control over territory just across the border in Syria. They are more disciplined and better armed than the tribal fighters drawn into the fray over the past week, and the Iraqi security forces lack the equipment and technology that enabled U.S. troops to suppress the al-Qaeda challenge.

In the past year, al-Qaeda has bounced back, launching a vicious campaign of bombings that killed more than 8,000 people in 2013, according to the United Nations. Sectarian tensions between Iraq’s Sunnis and the Shiite-led government have been further inflamed by the war in Syria, where the majority Sunni population has been engaged in a nearly three-year-old struggle to dislodge President Bashar al-Assad, a member of the Shiite Alawite minority.

Al-Qaeda’s ascendant influence in Syria has given the militants control over the desert territories spanning both sides of the ­Iraqi-Syrian border, enabling them to readily transfer weapons and fighters between the arenas.

In Syria on Friday, there were demonstrations in several rebel-held towns against ISIS’s presence, and in at least one town ISIS fighters opened fire on protesters, echoing the suppression of anti-government demonstrations by Syria’s government in the early days of the revolt. Clashes also erupted between the al-Qaeda-affiliated fighters and Islamist fighters from the newly formed Islamist Front in the rebel-held north, in a sign of growing tensions between Syrians and foreign-influenced extremists.

Most residents of Fallujah do not support the al-Qaeda fighters, the journalist there said, but they also lack the means to oppose them, and they also oppose the Iraqi government.

“It is sad, because we are going back to the days of the past,” he said. “Everyone is remembering the battles of 2004 when the Marines came in, and now we are revisiting history.”

Ahmed Ramadan and Loveday Morris in Beirut and Ben Van Heuvelen in New York contributed to this report.