When it was all said and done, the deal to release some of the hostages held by Hamas came down to two critical phone calls ultimately forcing each side to make a tough concession.

The Israelis were insisting that it was not enough to free just 50 of the roughly 240 hostages. They had to have more, they said. At that point, President Biden had to talk Prime Minister Benjamin Netanyahu of Israel into accepting what was on the table and then keep working to recover the rest.

As for Hamas, according to senior administration officials, its leaders were demanding that the pause in fighting incorporated into the agreement last five days, even though the Israelis refused to agree to more than four. Mr. Biden told the emir of Qatar, who was serving as the intermediary with Hamas, that four was all they would get for now.

The path to the hostage deal was painful and painstaking, one marked by fitful progress, deep mistrust, terrible choices and moments when the whole thing was on the verge of unraveling. Neither side got exactly what it wanted. But if the agreement is carried out successfully over the next few days — and that is still an important if — it could serve as a template for further negotiations to free more of the hostages and extend the temporary cease-fire.

“Last night’s deal is a testament to the tireless diplomacy and determination of many dedicated individuals across the United States government to bring Americans home,” Mr. Biden said on Wednesday on X, the platform formerly called Twitter. “Now, it’s important that all aspects are fully implemented.”

This account is based on senior Biden administration officials, who spoke on condition of anonymity to avoid disrupting the channels of communication.

The effort to free the hostages extends back to the hours following the terrorist attack of Oct. 7, when Hamas gunmen killed about 1,200 people and captured the other 240.

Shortly after the attack, the government of Qatar, a small Gulf emirate that hosts some Hamas leaders but maintains close relations with the United States, approached the White House with information about the hostages and suggested the possibility of a deal to win their release. The Qataris asked that a small group of U.S. officials work secretly with them and the Israelis.

Jake Sullivan, the president’s national security adviser, directed Brett McGurk, the White House Middle East coordinator, and Joshua Geltzer, then the deputy homeland security adviser who has since become the top lawyer for the National Security Council, to take the lead. To preserve secrecy, other agencies were kept in the dark about the initiative.

Mr. McGurk, who has wide contacts in the region, held early morning phone calls each day with the emir of Qatar, Sheikh Tamim bin Hamad al-Thani, and then briefed Mr. Sullivan, who kept Mr. Biden informed. Mr. Sullivan stayed in touch with Ron Dermer and Tzachi Hanegbi, two of Mr. Netanyahu’s closest advisers.