In a major reversal, Israeli security forces began removing controversial metal detectors early Tuesday that Prime Minister Benjamin Netanyahu had ordered placed at the entrances to the al-Aqsa Mosque compound in Jerusalem’s Old City.

The metal detectors angered Palestinians, who said the devices were not installed for their safety, but to increase Israel’s control over access to the mosque.

Netanyahu and his supporters said the metal scanners were needed after three Arab Israeli gunmen smuggled homemade machine guns into the al-Aqsa Mosque compound on July 14, then shot and killed two Israeli policemen at the site, which both Muslims and Jews regard as holy.

The removal of the devices appears to have been part of a deal struck between Netanyahu and Jordan’s King Abdullah II, who oversees the administration of the holy site as part of an agreement dating back to 1967.

In a telephone call on Monday, the king urged Netanyahu to “remove the cause” of the current crisis, according to Jordan’s official news service.

The unilateral decision by Netanyahu to set up the metal detectors had upset the Jordan monarch, the official custodian of the Jerusalem mosque, and sparked a surge of deadly violence and protests in East Jerusalem, the West Bank and Amman, Jordan’s capital.

The removal of the metal detectors began just a few hours after Israel and Jordan managed to end a diplomatic showdown.

On Sunday, a Jordanian workman of Palestinian descent used a screwdriver to stab and wound an Israeli security guard at the Israeli Embassy compound in Amman. The guard shot and killed the assailant and killed another Jordanian, reportedly an innocent bystander.

Fearing violence against its diplomats, Israel sought to have its embassy staff, including the guard, quickly leave Amman and return to Israel.

Jordan balked and initially blocked their departure, insisting that the guard first be interrogated about the killing. Israel claimed diplomatic immunity on his behalf.

Late Monday night, the Israeli diplomats and embassy staff, including the guard, were allowed to return to Israel.

The prime minister’s office denied that Jordan had demanded the detectors be removed as a condition of allowing the embassy staff to leave Amman for Israel.

Even so, hours after the embassy staff’s safe return, Israel’s security cabinet decided to remove the metal detectors and buttress security at the entrances to the mosque compound with high-tech cameras.

The decision by Israel to remove the scanners represents a win for the Palestinians, who had vowed not to pass through the detectors. Instead, Muslim worshippers gathered at Israeli police barricades and prayed outside.

The Palestinians say that their fury — manifested in mass protests and the killing of three Israelis at a Jewish settlement on Friday — is driven by fear that their sacred mosque is under threat.

For their part, many Israelis scoff at that kind of talk. They say the metal detectors were a normal, everyday security tool and that opposition to their use was just an excuse to incite violence against Israel.

Many Israelis and their leaders said that the root cause of the recent violence has little to do with metal detectors and is about Palestinian hatred for Israel and rejection of the very idea of a Jewish state.

The Palestinian U.N. ambassador, Riyad Mansour, warned on Monday that Palestinians will stay in the streets until the metal detectors and any other obstacles are removed, the Associated Press reported.

The United Nations’ Mideast envoy, Nikolay Mladenov, told reporters in New York that “the dangers on the ground will escalate” if Israel’s metal detectors are still in place for Muslim prayers on Friday.

The U.N. Security Council met behind closed doors Monday to discuss the violence in Jerusalem and Jordan and planned a second, open session on Tuesday.

“We are calling on all parties to refrain from any act or statement that could exacerbate tensions and to work toward easing the situation,” France’s U.N. ambassador, François Delattre, told reporters.

The site, known to Muslims as the Noble Sanctuary and to Jews as the Temple Mount, has been the scene of frequent clashes.

Following Palestinian protests, Jordan in 2016 abandoned a plan to install security cameras at the site and make the footage instantaneously available to Israeli and Jordanian security teams.

Seeking to help ease the soaring tensions, the Trump administration dispatched presidential envoy Jason D. Greenblatt to the region Sunday night, after a weekend of diplomatic outreach that involved telephone calls from Greenblatt, White House adviser and Trump son-in-law Jared Kushner and the U.S. ambassador in Israel, David Friedman.

A senior administration official said Greenblatt met Monday with Netanyahu and then flew to Jordan.

Greenblatt’s visit is open-ended, according to the official, who spoke on the condition of anonymity to describe the behind-the-scenes U.S. engagement.

Anne Gearan in Washington contributed to this report.