President Barack Obama announced two years ago he would curtail eavesdropping on friendly heads of state after the world learned the reach of long-secret U.S. surveillance programs.

But behind the scenes, the White House decided to keep certain allies under close watch, current and former U.S. officials said. Topping the list was Israeli Prime Minister Benjamin Netanyahu.

The U.S., pursuing a nuclear arms agreement with Iran at the time, captured communications between Netanyahu and his aides that inflamed mistrust between the two countries and planted a political minefield at home when Netanyahu later took his campaign against the deal to Capitol Hill.

The National Security Agency’s targeting of Israeli leaders and officials also swept up the contents of some of their private conversations with U.S. lawmakers and American-Jewish groups. That raised fears — an “Oh-s— moment,” one senior U.S. official said — that the executive branch would be accused of spying on Congress.

White House officials believed the intercepted information could be valuable to counter Netanyahu’s campaign. They also recognized that asking for it was politically risky. So, wary of a paper trail stemming from a request, the White House let the NSA decide what to share and what to withhold, officials said. “We didn’t say, ‘Do it,’ ” a senior U.S. official said. “We didn’t say, ‘Don’t do it.’ ”

Stepped-up NSA eavesdropping revealed to the White House how Netanyahu and his advisers had leaked details of the U.S.-Iran negotiations — learned through Israeli spying operations — to undermine the talks; coordinated talking points with Jewish-American groups against the deal; and asked undecided lawmakers what it would take to win their votes, according to current and former officials familiar with the intercepts.

This account, stretching over two terms of the Obama administration, is based on interviews with more than two dozen current and former U.S. intelligence and administration officials and reveals for the first time the extent of American spying on the Israeli prime minister.

An expanded version of this report appears on WSJ.com.