He then helped Moshe Kahlon, a former Likud minister, build Kulanu, a new center-right party that joined Mr. Netanyahu’s coalition after the 2015 elections. Mr. Gabbay became the minister for environmental protection, but he quit after a year, saying that he did not like what he saw in government and that Mr. Netanyahu’s decision to replace his defense minister, Moshe Yaalon, with the ultranationalist Avigdor Lieberman as part of a political deal was too much to swallow.

Mr. Gabbay joined Labor about six months ago.

Addressing a hall filled with cheering supporters at 11 p.m. shortly after the results were in, Mr. Gabby said, “To all those who rushed to eulogize the Labor Party as an alternative for the government, and to all those who thought the Israeli citizens had lost hope in change, to all those — tonight is the answer.”

“Tomorrow we will begin the journey to the hearts of good Israelis,” he added. “Israelis who believe in our ideology and values, but Israelis who, for decades, have not voted Labor.”

Mr. Gabbay and Mr. Peretz, 65, from the immigrant town of Sderot near the border with the Gaza Strip, are both Mizrahi, or Eastern, Jews of Moroccan descent, and their leadership contest brought to the fore the debate over Israel’s identity and ethnic politics. Labor has always been identified with the old Ashkenazic elite who hailed from Europe.

This is not the first time a Mizrahi, or Sephardic, Jew has headed the Labor Party. Mr. Peretz led it for a period in the past, as did Benjamin Ben-Eliezer, an Iraqi-born politician. Both Mr. Peretz and Mr. Ben-Eliezer were also defense ministers.

Mizrahi Jews, who immigrated mostly in the 1950s, were resentful of the sometimes highhanded treatment by the Labor establishment, so many have traditionally voted for Likud or other right-wing or religious parties. Though Mizrahim make up roughly half of Israel’s Jewish population, and about a third of Israeli children are now born into mixed Mizrahi/Ashkenazic families, economic and educational gaps remain.

Despite hopes in Labor that Mr. Gabbay will be able to bring in new voters from sectors of the public that have long shunned the party, Mitchell Barak, an Israeli pollster and political commentator, said his surveys over the years showed that the Mizrahim consistently preferred Ashkenazic candidates for prime minister.