A year ago it was out of the question. A month ago, it looked like a bad bargain for all concerned. A week ago, it loomed on the horizon like a prelude, rather than a substitute, for civil war.
And, yet, last Monday, Pakistanis turned up in millions to confound doomsayers by voting in what now looks like the country's first free and fair general election with results that few would contest.
The election took place against a background of threats by some of the deadliest terrorist groups in the world to kill anyone who participated. At the same time, powerful military figures plotted to rig its outcome.
The biggest winner is the Pakistani people that, given a chance, have always manifested attachment to pluralism and the rule of law.
Al Qaeda and its Taliban allies did all they could to prevent and then to disrupt the exercise. They killed some 300 candidates, election officers and party activists. Their sinister slogan "From Box to Box", meaning that anyone who casts a vote into the ballot box could end up in a coffin, was posted or scribbled on many walls. The terrorists also destroyed at least 12 polling stations and stile several dozen ballot boxes.
Still, they failed.
Their political allies, grouped in several so-called Islamist parties that fielded candidates did no better.
The Unified Assembly for Action (MMA), a coalition of Islamists that won almost 11 per cent of the votes in the last general election five years ago saw its share drop to around three per cent. It lost control of the only one of Pakistan's four provinces that it governed. Almost all its principal leaders lost their seats. In the provincial assembly of Sindh, the MMA won no seats.
Worse still, the Islamists' defeat in the Northwest frontier Province came at the hands of the avowedly secularist Awami League Party (ANP) which preaches a form of socialism.
A Shiite group, heavily financed by the Islamic Republic in Tehran, suffered an even bigger rout. Once the final results are tabulated, it could end up with one per cent of the votes.
The politicians associated with military and security agencies also lost, although not as heavily as the Islamists.
Their principal party, the Pakistan Muslim League (Q) lost almost two-thirds of its seats and with it control of the national parliament and the three provincial assemblies that t had dominated for years.
Among the casualties were the party's best-known leaders.
The message of this election is clear: the overwhelming majority of Pakistanis reject both military rule and its political twin of Islamism. The twins started dominating Pakistani politics in the 1970s when General Zia ul-Haq overthrew the democratically elected government of Prime Minister Zulfiqar Ali Bhutto in a military coup. Unable to build a popular political base, the general played the Islamist card, using religion as an ideological prop for a corrupt and brutal regime.
The Islamists played the role assigned to them in exchange for a share of political power, not to mention the looting of the national economy.
With last Monday's election, Pakistan returns to where it was in 1977, that is to say before Zia's coup.
Two mainstream movements, the conservative Pakistan Muslim League (PMLN) of former Prime Minister Nawaz Sharif and the Pakistan People's Party (PPP) of the Bhuttos have returned as principal players in national and provincial politics.
The PMLN, a right-of-centre party, is likely to form the next provincial government in Punjab, Pakistan's most populous province.
The PPP, a left-of-centre outfit, will control Sindh and is likely to lead a coalition government in Baluchistan.
Even before the final results were confirmed, there was much talk of the PPP and PMLN forming a government of national unity in a grand coalition.
The idea is attractive but unwise.
With the PMLN and the PPP in government together, the allies of the military and the remnants of the Islamists could cast themselves in the role of the opposition, peddling the message that non-military non-religious parties cannot solve Pakistan's social, economic and political problems.
The best scenario would be for the two mainstream parties, perhaps in association with a bloc of independents, to cover the entire political spectrum. One of the two parties could become the kernel of a coalition government while the other leads the parliamentary opposition. The model has worked in neighbouring India for more than half a century. There is no reason why it shouldn't work in Pakistan.
The PMLN and the PPP have different historical trajectories and provide alternative visions and programmes. These should not be blurred through a power-sharing scheme that may or may not be sustainable.
It would be better for both, and for Pakistan, if the two parties retain their distinct identities and offer voters a clear choice.
The formation of a people-based government has always been a basic condition for winning the war against terror in both Pakistan and Afghanistan.
That condition can now be fulfilled. In political terms, this means a strategic turning of the tide against the terrorists.
Nevertheless, the restoration of democratic rule on its own will not ensure victory.
The winners of the election would be unwise to spend their energies settling personal scores with President Pervez Musharraf.
If anything, they owe Musharraf a debt of gratitude. He is the only one of Pakistan's military rulers to organise free and fair elections and accept results that do not favour his camp. Despite a campaign of vilification against him, Musharraf still enjoys a capital of trust that could be employed in the service of what is, in effect, a new system of government based on separation of powers.
Musharraf has always claimed that he wishes to adopt the Turkish political model for Pakistan.
This means a system in which the armed forces act as ultimate guardians of the Constitution, preventing dictatorship in the name of either nationalism or faith.
The system has worked well for Turkey, even allowing it to experiment with an Islamist government in a secular state for almost five years now without being plunged into civil war.
The new Pakistani government must come out with a credible programme of social reform and economic development to give people hope. It must clean up the security services by getting rid of rogue elements pursuing personal agendas in the name of Islam.
The outgoing government did a lot of shadow boxing against the terrorists especially in the tribal areas bordering Afghanistan. This was because it needed Islamist support to compensate for its lack of a genuine popular base.
The new government, enjoying a broad support base, would have no need of such tactics. It should create a popular front against terror to meet a challenge that threatens its unity.
Pakistan's friends, notably the United States, the European Union and the moderate Muslim nations must dig deeper in their pockets to help in both economic and military terms.
A year ago, most Pakistanis didn't even see the tunnel their country was in. Now they can see light at the end of the tunnel.