Russia last year blocked exports of Ukrainian grain across the Black Sea, a vital part of the global food supply, but they resumed last summer under a fragile pact.

Ukraine and Russia agreed Wednesday to a two-month extension of a wartime deal that allows Ukraine to ship its grain across the Black Sea, a rare example of cooperation between the two countries.

The agreement was scheduled to expire on Thursday. While both sides expressed ongoing grievances over the issue of exports, reaching agreement appeared to offer advantages for governments in both Kyiv and Moscow, as well as for President Recep Tayyip Erdogan of Turkey, who acted as broker along with the United Nations.

Under the Black Sea Grain Initiative, which began last July, Ukraine, a major exporter of wheat, corn and sunflower oil, can transport grain and other food products along a corridor past Russian naval vessels that have blockaded Ukraine’s ports since the start of Russia’s full-scale invasion 15 months ago. The shipments are subject to inspection off the coast of Istanbul, while empty cargo ships headed to Ukraine’s ports are also checked, in part to ensure they are not carrying weapons.

Grain exports are important for Ukraine’s economy and their resumption also helps maintain the stability of global food prices, which rose sharply during the first months of war, as grain intended for export piled up in Ukrainian ports and warehouses. The resulting shortages and price increases raised the threat of famine in parts of the Middle East and Africa.

“These agreements matter for global food security,” the U.N. secretary general, António Guterres, said in a statement, adding that “Ukrainian and Russian products feed the world.” He said that discussions would continue on unresolved issues, which he did not specify.

Acrimony caused by the war is a backdrop to the agreement. Russian forces have seized control of agricultural land in southern and eastern Ukraine, and Russian missiles have also targeted Ukraine’s agricultural facilities.Oleh Uskhalo, a Ukrainian farmer, inside a destroyed warehouse in the Kherson region last month.Bernat Armangue/Associated Press

To add further insult, this month, Denis Pushilin, the pro-Russian leader of Donetsk region, which Moscow has illegally annexed, said authorities had loaded grain onto a ship at the port of Mariupol. Russian forces decimated the city last spring and occupied it one year ago after a siege.

Ukraine has complained in recent weeks that Russia has prevented required inspections of Black Sea shipments and refused to approve the use of more vessels.

Ukraine’s infrastructure minister, Oleksandr Kubrakov, said in a Facebook post that despite the extension until July 18, more work needed to be done to address shipment delays that he blamed on Russian “sabotage.”

“We hope that our partners will do their best to get the grain deal to fully work for the world’s food security and that Russia will eventually stop using food as a weapon and blackmail,” he wrote.

Several times over the past year, Russia has threatened to withdraw from the deal, arguing that provisions allowing its own agricultural products and fertilizers to be shipped to world markets are not being fulfilled.

The spokeswoman for Russia’s Foreign Ministry, Maria Zakharova, suggested that the extension of the agreement did not address Russia’s complaints. “The distortions in the implementation of the deal should be corrected as quickly as possible,” she said during a news conference.

Even though Russian agricultural products are not covered by Western sanctions, some international shipping companies have avoided getting involved in that trade because sanctions have made it more difficult to strike financial and insurance deals with Russia.

In fact, trade data shows that grain exports rose last year as Russia reported a record harvest. And while it’s not clear how much those exports have been facilitated by the grain deal, analysts say that Mr. Putin has been under pressure from two of his most important international partners — Turkey and China — to continue to renew it.

The deal also serves Mr. Erdogan, who faces a presidential runoff in 11 days, allowing him to avert a repeat of last year’s spike in food prices, and to project himself to voters as a vital international leader. He made little reference to his role in brokering the deal on the campaign trail, but supporters take pride in seeing him wheeling and dealing with other world leaders and, to that extent, the announcement enhances his image.

It also highlights Mr. Erdogan’s warm ties with President Vladimir V. Putin of Russia, which have grown stronger during the war, to the dismay of Turkey’s NATO allies.

Mr. Erdogan, who announced the extension in a speech to governing party officials, called the renewal “good news for the whole world.”

More than 30 million tons of food have been shipped under the Black Sea Initiative, according to data from the United Nations. The original deal was extended in October and again in March, but the last renewal was for just 60 days, half the 120 days sought by Turkey, Ukraine and the United Nations.

Exports slowed in recent weeks as the expiration date approached, and international aid agencies said they feared a lapse in the deal would affect millions of people worldwide. Over all, Ukrainian grain exports have fallen significantly since the invasion.

Since the Russian blockade began last February, Ukraine has explored ways to export more of its crops overland into Eastern and Central Europe. However, those routes can handle much smaller volumes — and have also provoked a backlash among farmers in some countries, after the Ukrainian grain flooded their markets, driving down prices.