AS elections go it, should have been a time of celebration.

This, after all, is a country which for half its turbulent history has been ruled by the military, but yesterday the nation experienced its first transition between civilian governments in 66 years. Yet no sooner had the polling stations opened than a string of militant attacks and gunfights killed at least 17 people, casting a long shadow over what had been hailed as a landmark election. It was always going to be so in Pakistan, a land caught between democratisation and jihadism.

In the searing heat, many of the 86 million people eligible to vote went to the polls excited about the prospect of change. However, brave as their efforts are to consolidate the democratic process, this vote comes right in the middle of Pakistan's most volatile crisis in decades.

In the almost 12 years since the US invaded neighbouring Afghanistan in response to the 9/11 attacks, Islamabad has seen its writ being eroded by an insurgency waged by Pakistani Taliban rebels who, unlike their Afghan counterparts, subscribe to the transnational jihadism of al-Qaeda. Knowing this, many Pakistanis turned out yesterday sensing the significance of the moment.

"The team we elect today will determine whether the rot will be stemmed or we slide further into the abyss," prominent lawyer Babar Sattar wrote in a major daily newspaper, The News International.

Sattar could have been forgiven for thinking that same slide into the abyss was gaining momentum yesterday as reports came in from the country's largest city, Karachi, where voters complained of irregularities and intimidation and the election commission said the process was flawed.

"We have been unable to carry out free and fair elections in Karachi," it admitted openly in a statement.

A major religious party, Jamaat-e-Islami, said it was pulling its candidates out of Karachi because of allegations of vote-rigging by its local rival.

Also in Karachi, a bomb attack on the office of the Awami National Party (ANP) killed 10 people and wounded about 30, while at least two more were wounded in three blasts that followed and gunfire could be heard in other parts of this teeming port city.

Elsewhere across the country, news of election troubles also filtered in, with four people killed in a gun battle in Baluchistan. Two more died near a polling station after gunmen on a motorcycle opened fire. In the insurgency-infected north-west, there were further casualties in a blast in the city of Peshawar at the foot of the famous Khyber Pass, on the border with Afghanistan.

For a long time politicians and their families here have faced the ongoing wrath of the Pakistani Taliban, as the terrorists kept their promises of spilling the blood of openly anti-Taliban parties. The government meanwhile – unlike the Taliban – is unable to keep its own promise of preventing such strikes and ensuring security.

This dire security situation has in turn compounded the country's chronic economic problems caused by instability, rampant corruption and political economic mismanagement. Electricity in many parts of Pakistan is in short supply and the treasury is nearly empty. Power cuts can last more than 10 hours a day in some places, crippling key industries such as textiles, and a new IMF bailout may be needed soon to rescue the economy.

In turn, these economic conditions fuel lawlessness and public dissatisfaction, especially with the outgoing Pakistan People's Party (PPP) government which, despite being able to complete its five-year term, has been unable to deliver on issues of governance.

Indeed, as the influential US current affairs magazine Foreign Policy pointed out recently, the stories of the three major contesting parties are a study in contrast. The PPP, led (from abroad) by the son of Benazir Bhutto, is the battered and besieged incumbent. The Pakistan Muslim League-Nawaz (PML-N), headed by former Prime Minister Nawaz Sharif, is the conventional favourite. The Pakistan Tehreek-e-Insaf (PTI), chaired by massively popular ex-cricket star and populist Imran Khan, is the upstart seeking to upset them both.

Khan appeals mostly to young urban voters because of his calls for an end to corruption, a new political landscape and a halt to US drone strikes on Pakistani soil. About one-third of the country's population is under the age of 30. The freshness of Khan's appeal was summed up by one voter yesterday.

"It's the first time I have voted," Rizwana Ahmed, 42, told reporters as she stood at a polling station near a slum in Islamabad waiting to cast a vote for Khan's party.

"I never felt like my vote counted before. It was always the same people or their families. Now there's someone new."

The PML-N looks set to win the most seats, but Khan could deprive Sharif of a majority and dash his hopes for a return to power 14 years after he was ousted in a military coup, jailed and later exiled. That said, many political analysts point to just how unpredictable the final outcome is.

Just days ago opinion polls suggested that disenchantment with the two main parties, the PPP and the PML-N, could mean that no one group emerges with a parliamentary majority, making the next government unstable and too weak to push through much-needed reform.

A late surge of support for Khan's PTI party has made a split mandate all the more likely. Khan, 60, is in hospital after injuring himself in a fall at a party rally, which may also have won him sympathy votes.

"The timing of such a split couldn't be worse for Pakistan," says Sattar. "The challenge of terror and economic meltdown confronting us won't wait for a party to be granted a clear mandate."

Results from nearly 70,000 polling stations nationwide started trickling in from around 10pm local time last night and both PML-N and PTI appeared well ahead of their rivals, according to a partial count.

Voters will elect 272 members of the National Assembly and, to win a simple majority, a party would have to take 137 seats. However, the election is complicated by the fact that a further 70 seats, most reserved for women and members of non- Muslim minorities, are allocated to parties on the basis of their performance in the contested constituencies. To have a majority, a party would need 172.

Despite Pakistan's history of coups, the army stayed out of politics during the five years of the last government and has thrown its support behind yesterday's poll. However, there are residual fears that the military could step back in if there is a repeat of the incompetence and corruption that frustrated many Pakistanis during the last government.

Another more unusual factor impacting on the election is the wave of attacks by jihadists and the Pakistan Taliban. They are trying to prevent the maturing of Pakistani democracy, which is a threat to their plans to turn the country into a launchpad for a global caliphate.

AND, of course, they are eyeing the Nato drawdown in neighbouring Afghanistan as an opportunity to enhance their regional influence: any political stability in Pakistan would hamper such ambitions.

Whatever the shape of Pakistan's next government and despite having its work cut out, it does at least have a number of things going for it. According to analysts at Foreign Policy magazine, among these is a "feisty democracy" that has proved robust and survived in the past despite considerable political pressure. It says the country has a great tradition of "activist judges" who engage in the political process whenever threatened, and that Pakistan finally has a much freer media. All, say analysts, act as bulwarks to protect the democratic process.

However, whatever the complexion of the next government, the biggest item on its agenda will be how to deal with the domestic insurgency that grips the country and manage the 2014 drawdown of Western forces in Afghanistan, especially since talks between Washington and the Afghan Taliban have failed to make headway.

The PML-N and the PTI have both said they will focus on negotiations with the Pakistani Taliban. Just how they will go about this is another matter, given that the jihadists have an agenda that contradicts the constitution of the country.

Both Khan and Sharif in their own ways have been pushing a "Save Pakistan" mantra. According to the independent US-based intelligence monitoring group, Stratfor, there is a growing realisation within Pakistan that if democracy does not lead to economic revival, circumstances will feed the growing public disillusionment with democratic governance. This, of course, is something the Pakistan Taliban and jihadists will do all they can to exploit to further their radical Islamist agenda.

Pakistan's next government will be under pressure like never before to deliver, for reasons beyond self-interest and re-election. Should it fail to respond favourably, there is the ever-present danger that the country's democratic system, no matter how robust, could finally be undermined, leading to the kind of "revolutionary" conditions Stratfor says would suit the jihadists' attempts to undermine an already-weakened state. Pakistan may have survived its landmark elections for now, but dangerous days lie ahead for the country and the region as a whole.