BAGHDAD — Iraq’s outgoing Prime Minister Haider al-Abadi, an American ally, and the radical Shiite cleric Moktada al-Sadr, a longtime foe of the United States, said on Saturday that they had become political allies in an effort to form a new government in the wake of widely discredited elections.

The announcement, made in the Shiite holy city of Najaf in a meeting between the two leaders, came as a surprise to many political observers, especially because Mr. Sadr — the top vote-getter — had already announced an alliance with a pro-Iranian Shiite leader, Hadi al-Ameri. Mr. Ameri was second in the polls, while Mr. Abadi was third.

“This is a call for an alliance that is nonsectarian and rejects ethnic politics in order to include all of the Iraqi people,” Mr. Sadr said at a joint news conference with Mr. Abadi, who is still the prime minister until the new parliament sits on July 1 and elects his replacement.

“We want to speed up the process of coming to a political agreement to send a positive message to the citizens that we are serious about moving forward,” Mr. Abadi said.