Hamas has agreed to release the remains of four Israeli hostages in exchange for hundreds of Palestinian prisoners, Israeli officials and the group said, resolving a dayslong impasse between the two sides.

The agreement comes as the first phase of a fragile cease-fire deal draws to a close. Negotiators have yet to reach terms to extend the deal into a more comprehensive truce, raising concerns that the fighting in Gaza could resume.

During the first phase, Hamas had agreed to free 25 Israeli hostages and hand over the bodies of eight more in exchange for more than 1,500 Palestinians jailed by Israel. On Saturday, Hamas released the last living captives set to be freed in the first phase, and Israel was supposed to release 620 Palestinian prisoners in return.

But Israel delayed the release of the prisoners, saying they would not be freed until Hamas committed not to subject hostages to “humiliating ceremonies” during future exchanges. That raised more questions about any next steps for the cease-fire.

Late on Tuesday night, Hamas announced that a deal had been reached for the simultaneous release of Palestinian prisoners in exchange for the remains of the four hostages. Omer Dostri, the spokesman for Israel’s prime minister, confirmed that an agreement had been reached, saying in a text message that the swap would take place on Wednesday night or Thursday.

Some of the Palestinian prisoners slated for release were convicted of deadly attacks against Israelis, while others — including minors — were held without charge.

The impending exchange could be the last in the first phase of cease-fire, leaving both Israelis and Palestinians in limbo. Around 27 hostages and the remains of more than 30 others are still in Gaza, according to the Israeli government.

It is unclear whether serious negotiations on the second phase of the agreement have even begun, let alone borne fruit.

Mediators have pressed both sides to agree to a second stage. Steve Witkoff, President Trump’s Middle East envoy, was expected to visit the Middle East on Wednesday in an attempt to move the talks forward. But Mr. Witkoff’s travel to the region has now been delayed, according to a U.S. official, who spoke on condition of anonymity to discuss his schedule.