RIYADH, Saudi Arabia — As voters in Iran danced in the streets, celebrating the landslide re-election of a moderate as president, President Trump stood in front of a gathering of leaders from across the Muslim world and called on them to isolate a nation he said had “fueled the fires of sectarian conflict and terror.”

That nation was Iran.

In using the headline address of his first foreign trip as president to declare his commitment to Sunni Arab nations, Mr. Trump signaled a return to an American policy built on alliances with Arab autocrats, regardless of their human rights records or policies that sometimes undermine American interests.

At the same time, he rejected the path taken by his predecessor, Barack Obama. Mr. Obama engaged with Iran to reach a breakthrough nuclear accord, which Mr. Trump’s administration has acknowledged Iran is following.

Mr. Trump has presented the shift as a reinvestment in historical alliances with friendly nations in order to fight extremism and terrorism. But the juxtaposition of the election in Iran and the gathering in Saudi Arabia seemed to highlight a reality of the Middle East that presidents have long wrestled with: how to choose partners and seek American interests in a region torn by sectarian splits and competing agendas.