Up to 25 million tonnes of wheat and other grain are in danger of rotting in Ukrainian ports. AFP
Here is what we know about the deal:
Joint command and control
A joint command and control centre will be set up in Istanbul to oversee smooth operations and resolve disputes.
Participants will include the two warring sides and officials from Turkey and the United Nations.
Western officials believe the centre will take three to four weeks to set up. This means grain might not start flowing at full speed again until the second half of August.
Its siting in Istanbul is both a nod to Turkey's central location and President Recep Tayyip Erdogan's key role in resolving a dispute which has often seemed intractable.
Ship inspections remained one of the most complex and politically charged parts of the agreement.
Mines have been laid in areas around Ukraine's main Black Sea ports with the aim of averting a feared Russian amphibious assault.
Ukraine also did not want Russia to board its vessels to check for the possible delivery of weapons when the ships returned to port.
Western officials said the four sides agreed that ship inspections -- demanded by Russia -- would be too difficult to conduct in the open seas.
The four parties would instead oversee them at one of the Turkish ports on their way back to Ukraine -- most likely Istanbul.
It is still not clear precisely who will be allowed to board the Ukrainian ships.
Officials say it was quickly decided that de-mining the area would take too long to relieve the threat of starvation spreading to some of the poorest parts of the world.
Western officials say the deal ensures that Ukrainians pilot their own ships along safe routes or "corridors" that avoid known mine fields.
Ukrainian vessels will lead the grain ships into and out of Ukrainian territorial waters.
Both sides also pledged not to attack ships on the way in or out.
This point has also been contentious: Ukraine has warned that it does not trust Russian promises because these have been broken repeatedly throughout the war.
"We do not trust them, even if they sign an agreement with the UN," Ukrainian delegation member Rustem Umerov told reporters.
"This is an aggressor country."
Any attacks or disputes at sea would go to the joint command and control centre in Istanbul for review.
The agreement threatened to unravel when Russian President Vladimir Putin announced this week that he also expected his own country's grain to be covered by the deal.
Russia has been beset by waves of sanctions on its shipping companies as well as on agricultural products such as fertiliser.
The US Treasury last week issued a clarification saying Russian fertiliser and "agricultural commodities" were not subject to trade restrictions.
The European Union on Wednesday also made an exception for Russian wheat and fertiliser.
The UN and Russia are due to sign a separate memorandum of understanding in Istanbul guaranteeing that grain and fertiliser cannot be directly or indirectly affected by sanctions.
The deal is due to be signed at 1330 GMT at Istanbul's lavish Dolmabahce Palace in the presence of Turkish President Recep Tayyip Erdogan and UN Secretary General Antonio Guterres.
The warring sides are expected to be represented by Russian Defence Minister Sergei Shoigu and Ukraine's Oleksandr Kubrakov.
The agreement is valid for 120 days and may be automatically renewed without further negotiations.
It covers Ukraine's Black Sea ports in Odessa and two neighbouring locations.
Officials believe 120 days should be enough to clear a backlog of up to 25 million tonnes of wheat and other grain stuck in Ukrainian ports.