JERUSALEM — Under persistent prodding from President Obama, Israel and Turkey resolved a bitter three-year dispute on Friday with a diplomatic thaw that will help a fragile region confront Syria’s civil war, while handing the president a solid accomplishment as he closed out his visit to the Middle East.

The breakthrough took place in the most improbable of surroundings: a trailer parked on the tarmac of Ben-Gurion International Airport. Moments before Mr. Obama left for Jordan, Prime Minister Benjamin Netanyahu telephoned the Turkish prime minister, Recep Tayyip Erdogan, and apologized for deadly errors in Israel’s 2010 raid on a Turkish ship that was trying to bring aid to Palestinians in Gaza.

After years of angrily demanding an apology, Mr. Erdogan accepted Mr. Netanyahu’s gesture, and both sides agreed to dispatch envoys to each other’s nations, having recalled them in 2011.

The president’s involvement, a senior American official said, was crucial to both leaders, which is why Mr. Netanyahu scheduled the call before Mr. Obama’s departure from Israel. Mr. Erdogan insisted on speaking to Mr. Obama first before the president handed the phone over to Mr. Netanyahu. In the end, the call produced a win-win for all sides.