When Kurdish forces recaptured the town of Sinjar from the Islamic State two years ago, the leader of Iraq's Kurdish region gave a triumphant speech on a mountainside, with a breathtaking view of plains behind him.
Only the Kurdish flag would ever fly here, he pledged.
But today, the Iraqi flag flutters in the town and across a swath of disputed territory in northern Iraq. The city of Kirkuk and the lucrative oil fields near it are now back in the hands of the federal government.
Kurdish leader Masoud Barzani's decision to hold an independence referendum, despite furious Iraqi and regional objections, has backfired spectacularly. Instead of paving the way to statehood, or boosting the Kurds' bargaining power in negotiations, it has triggered a humiliating reversal of fortunes for Iraq's Kurds.
Even as the United States joined the chorus of protestations, Barzani pushed ahead. But while the region united in its desire to curtail Kurdish ambitions, the Kurds themselves were divided, first spatting over whether to hold the vote and later split on how to deal with the fallout. Now the rift has grown wider still, with Barzani blaming the loss of Kirkuk on a deal cut by a wing of Kurdistan's other main party to allow Iraqi troops to enter.
But the Kurds' overwhelming endorsement of independence had pressured Baghdad to reassert its control.
"It was a miscalculation," said Mahmoud Osman, a veteran Kurdish politician who was the main negotiator on the region's autonomy agreement with Baghdad in 1970 after nearly a decade of revolt, led by Barzani's father, against the central government.
Osman said he warned Barzani in a meeting after the date for the vote was set that the population would only be disappointed because there was no way of attaining independence amid the regional objections.
"He was definite that it should be done and could be done, and it's good, and a new situation would arise and they would deal with it," said Osman, a longtime confident of the Barzani family. "Maybe he thought America would be different, maybe he thought Iran and Turkey would not be as bad. Maybe he got different reports from his advisers. His evaluation of the situation was different."
Turkey, Iran and Syria were deeply concerned that the vote would fan secessionist sentiment among their own Kurdish populations. Along with Baghdad, they have the power to completely besiege the landlocked region economically.
Why Barzani was so keen to push forward without laying the groundwork is "anyone's guess," Osman said.
Barzani's opponents say he needed to bolster his popularity. In 2013, the regional parliament had extended his eight-year term by two years. But elections still haven't been held, and the region's electoral commission said the new developments have halted preparations for a vote due in November. Barzani had said he wouldn't run for reelection.
"This was a last, desperate attempt by the Kurdish leader to reclaim legitimacy by playing the independence card," said a Kurdish politician from neither main party who spoke on the condition of anonymity because he was criticizing the leadership.
Osman said he thought Barzani believed the vote would add to Kurdistan's negotiating power with Baghdad and the region. "It's the opposite," he said. "He thought about the best possible outcome, but not the worst."
Officials from Barzani's Kurdistan Democratic Party counter that Baghdad's actions were inevitable. When Iraqi forces collapsed en masse in the face of the Islamic State's rampage across northern Iraq in 2014, Kurdistan's government took full control of Kirkuk and the oil fields surrounding it, which pump more than half a million barrels a day. It gradually won other towns and villages back from the militants, expanding the region's borders.
The land grab meant that even before the referendum, Iraqi Prime Minister Haider al-Abadi was under pressure to curb Kurdish expansionism.
"I think what the referendum did do was maybe accelerate Baghdad's action," said one KDP official, who spoke on the condition of anonymity because the topic is sensitive.
The State Department said it strongly opposed the vote. For the United States, it would politically weaken Abadi, another of its key allies, ahead of elections next year.
The Trump administration attempted to give the Kurdish leadership a face-saving way to postpone the referendum. In exchange, one U.S. official said, the Kurds would receive letters from the United States and Britain promising to facilitate and support the Kurds' negotiations with Baghdad. The U.S. offer said that if negotiations with Baghdad had not progressed after two years, the United States would recognize the need for a referendum.
Saadi Pira, a spokesman for the Patriotic Union of Kurdistan party (PUK), the main opposition party to Barzani's, said he had just returned from Baghdad when a draft letter from Secretary of State Rex Tillerson was floated. Gauging the level of opposition to the vote in the capital, he said members of his party told Barzani that they supported the U.S. initiative, though some wanted stronger wording in the letter.
But Barzani said that if the PUK didn't back the referendum, he would hold the vote in KDP-controlled areas anyway, Pira said. That would cause an untenable rift in the region, Pira added.
Hemin Hawrami, an adviser to the president, said that the allegation that Barzani had threatened to go ahead without the PUK was "disinformation" and that all parties were behind the decision to hold a referendum.
The KDP official said the American initiative was hollow. "They were promising things that they couldn't implement because there would be opposition in Baghdad," he said. "What's the point? We've seen this play out so many times before."
The text of the letter was never agreed upon. Two U.S. officials said Tillerson never sent a letter to Barzani but did not deny that a draft was written. "There were certainly deliberations between Baghdad and Washington and other allies over potential ways forward," one said.
Iranian efforts to intervene also failed. Qasem Soleimani, Iran's military point man in Iraq, traveled to the Iraqi Kurdistan capital of Irbil before the referendum to persuade Barzani to stand down, Osman said.
The vote went ahead, with euphoric celebrations in the streets of Irbil and other Kurdish cities.
"It was a mistake, a big mistake, very big," Pira said. "At that time we started to negotiate to speak with the framework of logic. Now the war machinery has started to speak."
During the visit to Baghdad, Abadi offered concessions, including rights over airspace, in exchange for dropping the vote.
Barzani's camp countered that the mistake — a "monumental betrayal," the KDP official said — came from members of Pira's party, and that what unfolded was firmly their responsibility.
As Iraqi forces massed on Kirkuk's border this month, an emergency meeting was held in the lakeside city of Dokan. Iraqi President Fouad Massoum, from the PUK party, presented a deal that would allow Iraqi troops into Kirkuk, according to one attendee, who spoke on the condition of anonymity to discuss a private meeting. Barzani left in a fury.
At a joint news conference after the meeting, the two Kurdish parties said they were united and wouldn't negotiate unilaterally.
But the deal had already been done.
Speaking in a phone interview immediately afterward, Mala Bakhtiyar, a PUK official, said a deal for troops to enter had been forwarded to Baghdad through a "broker" that he declined to name, saying it had KDP approval, a charge the party vehemently denies.
"It was raised as an option but not agreed to," the official in Barzani's party said. "They promised they hadn't made a deal with the Iraqis."
Much of the leadership of the PUK was not even informed, Pira said.
Pira said some members of his party, dubbed the "Friends of Kirkuk" and led by the son and widow of recently deceased former president Jalal Talabani, made the deal secretly. They used a condolence ceremony for Talabani in Baghdad as an opportunity to discuss the deal with officials in the capital, he said.
The PUK has traditionally had stronger ties with Iran than has Barzani's party. Soleimani, the leader of the Iranian Quds Force and a major power broker in Iraq, attended another funeral ceremony for Talabani in the city of Sulaymaniyah and met with the family as the behind-the-scenes negotiations continued.
"They made a deal with Qasem Soleimani," said another senior KDP official who spoke on the condition of anonymity because the subject is a sensitive one.
Pira said the deal allows for joint administration of Kirkuk and for a 700-member peshmerga force to stay in the province along with local Iraqi police and Kurdish security forces. However, the Kurds would have to turn over oil fields, military bases, an air base and army bases. An Iraqi with knowledge of the negotiations confirmed the joint administration in return for the handover, but not that peshmerga would be allowed to remain.
He said the negotiating group made other demands, including salaries for select peshmerga forces and the reopening of international flights to Sulaymaniyah, which were rejected by Baghdad.
In a written statement on Tuesday, his first since Kirkuk fell, Barzani blamed the "unilateral decision" of "some elements" in another party for the events in Kirkuk. Since then, forces loyal to Barzani have also withdrawn from other disputed territories without a fight.
Pira said that a deal was necessary to avoid bloodshed and that economic pressure from the region could bring Kurdistan, which is already struggling to pay salaries, to its knees.
There was still some fighting as Iraqi forces entered Kirkuk, but many peshmerga were ordered to stand down in line with the deal.
"We have betrayed Kirkuk, we have betrayed Kurdistan," said Lt. Burhan Rashid, a peshmerga fighter.
Just days earlier, Rashid had been bolstering defenses near the city, vowing to fight any attempt by the Iraqi forces to break in on the other side of the bridge. Next to him at the bridge were the names of Kurdish fighters who had died in battles against the Islamic State.
"We have betrayed the blood of the martyrs written on that wall," he said.
Baghdad originally said it wanted to return to its 2014 positions, when Kurdistan began using the fight against the Islamic State to expand its borders. Iraqi militia leaders have expressed a desire to push farther, returning Kurdish forces to where they were in 2003.
Iraqi and Kurdish forces clashed on the Kirkuk road Friday, as federal troops tried to extend their line.
"It's imperative that Baghdad will not overreach," said Barham Salih, a Kurdish politician who recently left the PUK. "If they push too far, this could lead to a protracted conflict."
In the city of Kirkuk, residents worry about the impact of the instability. "They all sold it," Botan Aziz, 31, said of his city. "We don't want any of these parties."
Kareem Fahim in Istanbul and Karen DeYoung and Carol Morello in Washington contributed to this report.