Early in the sixteenth century, as the Ottoman and Safavid empires fought for control of the Middle East, Selim the Grim ruling from Istanbul indulged his artistic side by composing distinguished poetry in Persian, then the Middle East's language of high

Early in the sixteenth century, as the Ottoman and Safavid empires fought for control of the Middle East, Selim the Grim ruling from Istanbul indulged his artistic side by composing distinguished poetry in Persian, then the Middle East's language of high culture. Simultaneously, Ismail I ruling from Isfahan wrote poetry in Turkish, his ancestral language.

Selim the Grim (r. 1512-20) wrote poetry under the name Mahlas Selimi; his arch-rival Ismail I (r. 1501-24) wrote poetry as Khata'i.

This juxtaposition comes to mind as the populations of Turkey and Iran now engage in another exchange. As the secular Turkey founded by Atatürk threatens to disappear under a wave of Islamism, the Islamist Iranian state founded by Khomeini apparently teeters, on the brink of secularism. Turks wish to live like Iranians, ironically, and Iranians like Turks.

Turkey and Iran are large, influential, and relatively advanced Muslim-majority countries, historically central, strategically placed, and widely watched; as they cross paths, I predicted back in 1994, racing in opposite directions, their destinies will affect not just the future of the Middle East but potentially the entire Muslim world.

That is now happening. Let's review each country's evolution:

Turkey: Atatürk nearly removed Islam from public life in the period 1923-38. Over the decades, however, Islamists fought back and by the 1970s they formed part of a ruling coalition; in 1996-97, they even headed a government. Islamists took power following the strange elections of 2002, when winning a third of the vote secured them two-thirds of the parliamentary seats. Ruling with caution and competence, they got nearly half the vote in 2007, at which point their gloves came off and the bullying began, from a wildly excessive fine levied against a media critic to hare-brained conspiracy theories against the armed forces. Islamists won 58 percent of the vote in a September referendum and appear set to win the next parliamentary election, due by June 2011.

Atatürk excluded Islam from Turkey's public life and Khomeini made it central in Iran's.

Should Islamists win the next election, that will likely establish the premise for them to remain enduringly in power, during which they will bend the country to fit their will, instituting Islamic law (the Sharia), and building an Islamic order resembling Khomeini's idealized polity.

Iran: Khomeini did the opposite of Atatürk, making Islam politically dominant during his reign, 1979-89, but it soon thereafter began to falter, with discordant factions emerging, the economy failing, and the populace distancing itself from the regime's extremist rule. By the 1990s, foreign observers expected the regime soon to fail. Despite their populace's growing disillusionment, the increased sway of the Islamic Revolution Guards Corps and the coming to power of hardened veterans of the Iran-Iraq war, as symbolized by Mahmoud Ahmadinejad, imbued it with a second wind.

This reassertion of Islamist goals also increased the people's alienation from the regime, including a turn away from Islamic practices and toward secularism. The country's growing pathologies, including rampant drug-taking, pornography, and prostitution point to the depths of its problems. Alienation sparked anti-regime demonstrations in the aftermath of fraudulent elections in June 2009. The repression that followed spurred yet more anger at the authorities.

A race is underway. Except it is not an even competition, given that Islamists currently rule in both capitals, Ankara and Tehran.

Erdoğan and Ahmadinejad, in sync at last.

Looking ahead, Iran represents the Middle East's greatest danger and its greatest hope. Its nuclear buildup, terrorism, ideological aggressiveness, and formation of a "resistance bloc" present a truly global threat, ranging from jumping the price of oil and gas to an electro-magnetic pulse attack on the United States. But if these dangers can be navigated, controlled, and subdued, Iran has a unique potential to lead Muslims out of the dark night of Islamism toward a more modern, moderate, and good neighborly form of Islam. As in 1979, that achievement will likely affect Muslims far and wide.

Contrarily, while the Turkish government presents few immediate dangers, its more subtle application of Islamism's hideous principles makes it loom large as future threat. Long after Khomeini and Osama bin Laden are forgotten, I venture, Recep Tayyip Erdoğan and his colleagues will be remembered as the inventors of a more lasting and insidious form of Islamism.

Thus may today's most urgent Middle Eastern problem country become tomorrow's leader of sanity and creativity while the West's most stalwart Muslim ally over five decades turn into the greatest source of hostility and reaction. Extrapolation is a mug's game, the wheel turns, and history springs surprises.

Mr. Pipes is director of the Middle East Forum and Taube distinguished visiting fellow at the Hoover Institution of Stanford University.

Nov. 30, 2010 updates: Two points that did not fit in the main body of my column

(1) Ankara and Tehran work together ever closely these days but I predict that they will soon be rivals for Islamist leadership. Historical pride, sectarian ambition, and geo-strategic competition all suggest that the current moment of harmony will not last long. Look for the Turks to dispute Iranian leadership in such arenas as commercial prowess, military power, and religious potency.

(2) I sketched out this rivalry in a 1994 article in the National Interest, "[Turkey vs. Iran and] Islam's Intramural Struggle," in which I noted "a long, deep, and difficult fight" likely brewing "between two of the great countries of the Middle East, Turkey and Iran." Turks , I wrote, "seem not yet to realize what the mullahs know: that fundamentalist Islam will rise or fall depending on what Turks do, and that Iran and Turkey are therefore engaged in a mortal combat. Will Turks wake up in time to hold their own? Much hinges on the result." At this point, it appears that Turks did not wake up but, rather, Iranians did.