Inside the painstaking negotiations to agree on a deal allowing foreigners to leave Gaza

The breakthrough that allowed an initial group of foreign nationals, including US citizens, to depart Gaza on Wednesday came together after weeks of intensive, multi-party diplomatic efforts, sources familiar with the negotiations told CNN.

The agreement to allow foreign passport holders and a group of critically injured civilians to depart through the Rafah border crossing was reached on Tuesday, prior to the Israeli forces’ bombing of Gaza’s largest refugee camp.

Qatar, which coordinated with the United States, was the key broker of the deal between Israel, Egypt, and Hamas, according to sources familiar with the talks.

The development was hailed as a critical first step in getting thousands of foreign nationals out of the war-torn strip as Israel intensifies its military operations there. Though US officials have stressed that the situation remains fluid, they have expressed optimism that hundreds more will be able to depart in the coming days.

The negotiations were consistently described as immensely complicated, and the breakthrough came after “intense and urgent American diplomacy with our partners in the region,” President Joe Biden said Wednesday.

Negotiators had to contend with Hamas’ control of the Gaza strip, Israel’s blockade and bombing, as well as Egyptian security concerns.

Leading up to Wednesday, there had been a number of moments where US officials thought they would be able to get the Americans out, and the State Department had advised Americans to consider making their way toward the crossing. Those ultimately fell through, leading to frustration, fear, and confusion for the hundreds of US citizens trapped in Gaza.

US officials, led by Ambassador David Satterfield, engaged in on-the-ground diplomacy in both Israel and Egypt, but relied on partner countries to communicate with Hamas.

“We’re dealing with Israel, Egypt, and Hamas, and we’re not talking directly to Hamas, Egypt can send messages to Hamas, Qatar can send messages to Hamas. But you can imagine how difficult every little thing is, every bit of this is complicated,” a State Department spokesperson said last week.

At the outset of the negotiations, Egyptian President Abdel Fattah el-Sisi insisted that humanitarian aid make its way into Gaza before Egypt would consider letting civilians depart. Egypt had also made clear they would not accept a flood of refugees.

Since Hamas’ attack on Israel on October 7, Egyptian officials have expressed serious concerns about a scenario where Palestinians in Gaza are permanently displaced to Egypt. That anxiety had been a major complicating factor, as negotiations in part centered around Hamas’ request to allow wounded Palestinians to leave Gaza, two sources familiar said.

Hamas insisted that wounded Palestinians be able to leave alongside foreign nationals. Notably, the group wanted some of its own fighters to be included in the group of injured allowed into Egypt – a demand that was rejected, a senior US official said.

However, the US supported allowing injured civilians to depart and seek medical care, and a significant part of the US’s diplomatic efforts focused on convincing Israel to accept a list of wounded people that could leave, the official said.

Concerns about Palestinian displacement was a topic of discussion for Biden and Sisi – the leaders agreed that it was important to ensure that “Palestinians in Gaza are not displaced to Egypt or any other nation,” the White House said over the weekend.

Egypt wanted an international organization to serve as administrator and vet people before their departure from Gaza, a source familiar told CNN. The negotiators worked with the UN and eventually got them to agree to serve in that role. However, they could not get guarantees from Hamas that they would not harass or interfere with the UN officials, nor would they agree to allow them to operate on the Gaza side of the border, which is what Egypt wanted.

Instead, Hamas wanted the UN officials to be on the Egyptian side of the border, which Cairo would not accept because of security concerns.

Despite days of effort, the negotiators could not get Hamas to agree to what Egypt wanted, so the focus turned to other potential routes out. The negotiators were cognizant that time was not on their side. The Israelis agreed that they would allow foreign nationals to depart via Kerem Shalom, but Hamas continued to be the issue. There had been attempts by some countries to get small groups of civilians out via that route, but they had been blocked by Hamas.

In recent days, Egypt dropped its demand to have a third-party administrator oversee the Rafah gate, and Hamas – who had been engaging with Qatar – agreed to allow its border authority to operate the gate.

And as negotiations progressed and aid trucks were able to cross into Gaza, Egypt became a more cooperative player. According to one source, Egyptian officials had asked US officials for lists of American citizens and relatives who are seeking to leave Gaza. Miller said Wednesday that Cairo had “agreed to the transit people through Rafah some number of weeks ago, and it’s taken this long to actually make it a reality.”

It was Hamas, according to US officials, that was obstructing any movement. State Department spokesperson Matt Miller said last week that “at times, Hamas has had no one there manning the border station,” and “at other times, we have seen Hamas militants actively there with guns preventing people from approaching the crossing.”

National security adviser Jake Sullivan said on Sunday that Hamas had been “making a series of demands” to allow departures but would not go into details.

Still, over the weekend, a deal looked to be within reach. Biden again got on the phone with Israeli Prime Minister Benjamin Netanyahu and Sisi on Sunday to discuss the details of a possible agreement deal, officials told CNN.

On Monday, Secretary of State Antony Blinken spoke by phone with his Qatari counterpart Mohammed bin Abdulrahman Al Thani and strongly pressed him to push Hamas to accept the deal.

And it was the next day that the US “got to the point where we were able to feel confident that we could get American citizens out,” Miller said Wednesday.

Therefore the Hamas-controlled border authority is overseeing the departure of civilians through the gate on the Gaza side, as they did prior to the attack on October 7.

Approximately 400 American citizens and their family members – about 1,000 people total – are stuck in Gaza and are seeking to leave, as well as approximately 5,000 other foreign nationals, Blinken said on Tuesday.

Biden said Wednesday that as many as 1,000 more foreign nationals could depart soon via the Rafah crossing.

For more CNN news and newsletters create an account at CNN.com