* Russia and Ukraine reached deal with Turkey, sources say

* But Ukraine pulled out at last moment - sources

* Deal would have given safety to merchant shipping

* Turkey's Erdogan was about to announce it on March 30

MOSCOW/ANKARA, April 15 (Reuters) - Russia and Ukraine negotiated for two months with Turkey on a deal to ensure the safety of shipping in the Black Sea and reached agreement on a text that was to be announced by Ankara but Kyiv suddenly pulled out, four people familiar with the matter told Reuters.

The negotiations were mediated by Turkey after nudging by the United Nations, according to the sources who spoke to Reuters on condition of anonymity due to the sensitivity of such talks.

A deal was reached in March "to ensure the safety of merchant shipping in the Black Sea", and though Ukraine did not want to sign it formally, Kyiv gave its assent for Turkish President Tayyip Erdogan to announce it on March 30, the day before critical regional elections, the sources said.

"At the very last minute, Ukraine suddenly pulled out and the deal was scuttled," said one of the sources.

Three other people confirmed that version of events. Russia, Ukraine and Turkey declined to comment.

It was not immediately clear why Ukraine pulled out. The people who spoke to Reuters said they did not know what had prompted Kyiv's decision.

Ukrainian President Volodymyr Zelenskiy said in February that without new U.S. military aid, Ukraine would not be able to defend a Black Sea shipping corridor hugging its western Black Sea coast near Romania and Bulgaria.

The talks on the shipping deal, which have not been reported before, offer a glimpse of the quiet diplomacy going on behind closed doors on ways to bring the two warring sides to negotiation, if only, at first, about merchant shipping.

When asked for a comment on the Reuters reporting, United Nations Spokesman Stephane Dujarric said: "We still hope that freedom of navigation in the Black Sea will prevail."

Turkey and UN Secretary-General Antonio Guterres have been trying to months to get merchant shipping sailing more freely though the Black Sea, which in some areas has been turned into a naval war zone since Russia invaded Ukraine in 2022.

The Black Sea is a key route for both Russia and Ukraine to get bulk products such as grain, fertiliser and oil to world markets, though sea shipping volumes have fallen significantly since the war.

RUSSIA-UKRAINE TALKS

The text of the deal, a copy of which Reuters has seen, said that Turkey "as part of its mediation efforts" had reached agreements with Ukraine and Russia "on ensuring free and safe navigation of merchant vessels in the Black Sea" in compliance with the Montreux Convention of the Regime of the Straits.

That 1936 deal gives Turkey control over the Bosphorus and Dardanelles and the power to regulate transit of naval warships.

It also guarantees the free passage of civilian vessels in peacetime and restricts the passage of ships not belonging to Black Sea countries.

Under the deal almost announced on March 30, both Moscow and Kyiv would have offered security guarantees to merchant vessels in the Black Sea, committing not to strike or to seize or search them as long as they were either empty or had declared a non-military cargo.

"These guarantees do not apply to warships, civilian vessels carrying military goods (with the exception of maritime transportation agreed upon by the Parties within the framework of international missions)," the draft agreement said.

"The Republic of Turkey informs the UN Secretary General that the agreement has been reached and is being implemented through the mediation of the Republic of Turkey," the draft said. "The agreement comes into force upon announcement."

Turkey and the United Nations helped mediate the Black Sea Grain Initiative, a deal struck in July 2022 that had allowed the safe Black Sea export of nearly 33 million metric tons of Ukraine grain.

Russia withdrew from the agreement in July 2023, complaining that its own food and fertiliser exports faced serious obstacles. (Writing by Guy Faulconbridge in Moscow, Reporting by Tuvan Gumrukcu in Ankara and Michelle Nichols at the United Nations; Additional reporting by Gabrielle Tétrault-Farber in Geneva Editing by Tomasz Janowski)