Supporters of the Nida Tunis party listen to speakers at a rally just west of the city in Tunis, Tunisia, Oct 23, 2014. (Michael S. Williamson/The Washington Post)

More than 20 years ago, the scholar Samuel Huntington established his “two-turnover test” for fledgling democracies. A country can be said to be a consolidated democracy, he argued, only when there have been two peaceful transitions of power. This week, with its second parliamentary election, Tunisia passed Huntington’s test.

Tunisia’s relative success is a marked contrast to the abysmal failure of Egypt, the Arab world’s largest and once most influential country. As in Tunisia, Egyptians also overthrew a dictator three years ago. But after a brief experiment with democracy, in which the Muslim Brotherhood was elected and then abused its authority, today the country is ruled by a dictatorship. I recently asked a secular, liberal Egyptian from Cairo who was involved in the uprising against Hosni Mubarak whether the current regime feels like a return of the old order. “Oh, no,” he said. “This one is far more brutal, repressive and cynical than Mubarak’s.” On Monday, Egypt’s president, Abdel Fatah al-Sissi, issued a decree allowing the trial of more civilians in military courts.

Why did Tunisia succeed where Egypt failed? Analysts of the two countries have offered lots of answers, but the most common is that Tunisia’s Islamists were just better than Egypt’s. In both countries, Islamist parties won the first election. But as many commentators have pointed out, Tunisia’s Ennahda party, which is a rough equivalent of Egypt’s Muslim Brotherhood, sought to share power, while their Egyptian brethren did not. Ennahda has not tried to institute sharia, has declared its respect for Tunisia’s progressive laws on women’s rights and voluntarily ceded power this year to a technocratic, national unity government when faced with popular protests. The lesson seems to be that Tunisia was just lucky: Its Islamists were the good guys, the exception to the rule that Islamists are theocrats whose commitment to democracy extends only so far as one man, one vote, one time.

But Tarek Masoud , the author of a fascinating new book on Islamists and elections titled “Counting Islam,” suggests that Tunisia’s success and Egypt’s failure have less to do with the qualities of its Islamists than with deep differences in those countries’ political environments. In Egypt, Masoud argues, Islamists were able to defeat secular parties in the first elections after Mubarak was deposed because they could piggyback on the country’s rich network of mosques and Islamic associations to reach everyday citizens. Secular parties didn’t have anything equivalent. And so, after losing election after election, they turned to the army to overturn the results of the ballot box.

Tunisia was a different story, Masoud says. More developed, more urban, more literate and more globalized than Egypt, Tunisia had a more diverse civil society than Egypt’s — stronger labor unions, civic associations, professional groups — so there was relative parity between Islamists and their opponents. Though Islamists did well in Tunisia’s first elections, so did non-Islamists. Ennahda won only a plurality in the country’s first freely elected legislature — far less than the majority won by Islamist parties in Egypt — and had to govern in coalition with two secular parties. It shared power not because it was nicer than the Muslim Brotherhood but because it had to. And Ennahda’s opponents stuck with the democratic game even after losing, instead of calling on the army, because they, unlike the Egyptian secular parties, rightly felt they had a chance of winning in the future — as they did this week. (Tunisia is fortunate in that its army has always been subordinate to civilian authority.)

In 1939, Walter Lippmann said that the endurance of democracy rests upon a “sufficiently even balance of political power” between government and opposition, so that the former does not become “arbitrary” and the latter “revolutionary and irreconcilable.” Masoud notes that that balance of power existed in Tunisia but not in Egypt. “The many testimonials to the foresight of Tunisia’s political leaders, the moderation of its Islamists, and the respect of its soldiers for civilian institutions obscure a much more basic fact,” he says. “Tunisia offered more fertile terrain for pluralism.”

Of course, it may be too soon to celebrate Tunisia’s success. It faces a youth unemployment rate of about 30 percent. The government is also battling Islamist militants at home, and recent reports have suggested that the Arab world’s only democracy is also its biggest exporter of fighters to join the Islamic State. (This may be because Tunisia is relatively open and its jihadis find that their appeal is limited at home.)

But Tunisia’s success — so far — does suggest that there is nothing in Islam or Arab society that makes it impossible for democracy to take root. As would be true anywhere, you need some favorable conditions, good leadership and perhaps a bit of luck.

Read more from Fareed Zakaria’s archive, follow him on Twitter or subscribe to his updates on Facebook.