LAHORE, Pakistan -- Former Prime Minister Nawaz Sharif, once a political exile deposed by the military, returned to the cusp of power on Saturday, taking a commanding lead in a parliamentary election in which Pakistanis braved Taliban intimidation to cast ballots with historic prospects for the country's democracy.
Record turnout was reported in several cities, incited by an energized political campaign dominated by the battle between Mr. Sharif and Imran Khan, the former cricket star whose appeal as an anticorruption crusader had many predicting he could play a kingmaker role.
But even with just partial returns in early Sunday, Mr. Sharif's party appeared to have secured enough seats to form a government easily. His supporters ran cheering through the streets of Lahore, honking horns and, in some instances, firing bursts of celebratory gunshots.
While the raucous election highlighted the vibrancy of Pakistani politics, it also drew attention to the gaping holes in the country's democracy.
Threats by the Taliban to disrupt voting were borne out in attacks across the country that left at least 21 people dead, including at least 11 in a bombing in Karachi and others in Baluchistan Province, where turnout greatly suffered.
Accusations of widespread vote irregularities in Karachi, the nation's largest metropolis, led to the invalidation of results from dozens of polling places, officials said. Final results are likely to wait for days.
Even if Mr. Sharif faces no obstacles in forming a government, he will have to deal with a stalled economy, profound infrastructure failure and grave threats from an emboldened Taliban insurgency. Furthermore, he has promised to rein in American influence in Pakistan, leaving questions about the countries' often-stormy relationship.
The election was Pakistan's 10th since 1970 but the first in which a civilian government that has served a full five-year term is poised to peacefully hand power to another elected government.
Unlike previous elections, in which the military's Inter-Services Intelligence Directorate was widely accused of vote manipulation and intimidation, this one offered little evidence of involvement by the military, which has ruled Pakistan directly for more than half its 66-year history.
Instead, the country was gripped by election fever, most of it driven by the contest between Mr. Sharif and Mr. Khan. In the final days of campaigning, the momentum appeared to be with Mr. Khan, who electrified voters with a series of mass rallies that tapped into a deep vein of support among young and middle-class Pakistanis in urban areas.
His ratings rose further after he fell nearly 15 feet to the ground at a rally in Lahore on Tuesday, badly injuring his back but winning widespread public sympathy.
But as the results flooded in late Saturday, and television projections gave Mr. Sharif up to 119 of the 268 elected seats on offer, against just 33 for Mr. Khan, promises of a revolutionary "tsunami" led by the former cricket player appeared to have vanished.
That result signaled a victory of sorts for old-style dynastic politics: the Sharifs have dominated Punjab, Pakistan's most populous province, since the 1980s, and have cultivated voters for the past five years through development projects financed by the provincial government, which they controlled.
The other loser was President Asif Ali Zardari's Pakistan Peoples Party, which led the last government but now seems destined to the opposition benches.
Mr. Khan, however, was poised to capture a valuable consolation prize, one with potentially sharp implications for American policy: control of the provincial government in Khyber Pakhtunkhwa, along the Afghan border.
During his campaign rallies, Mr. Khan stridently said he would end C.I.A. drone strikes in the tribal belt, by ordering the Pakistani military to shoot down American aircraft if necessary. And he has said he believes that the Pakistani state should negotiate with Taliban insurgents, not fight them.
Still, it is Mr. Sharif who is likely to have the greatest impact on relations with the United States. A nationalist by inclination, Mr. Sharif, while publicly amenable to reaching out to the Americans, has also hinted that he was open to negotiating with Taliban rebels in the northwest -- a stance that would greatly concern American officials as they try to withdraw from an Afghan war next door that has featured many cross-border attacks by those insurgents.
A conservative and a steel baron, Mr. Sharif was mentored in his political career by a military dictator, Gen. Mohammad Zia ul-Haq, in the mid-1980s. But he turned against the generals after his ouster at the hands of Gen. Pervez Musharraf in 1999, which resulted in a long exile in Saudi Arabia. Since then, many military men have viewed Mr. Sharif with suspicion.
He first came to American attention during Pakistan's tense confrontation with India in 1999, when the possibility of a nuclear conflict was averted thanks to mediation by President Bill Clinton.
The election evoked a rare sense of enthusiasm for politics in Pakistan. About 4,670 candidates fought for 272 directly elected seats in Parliament, while almost 11,000 people battled for the four provincial assemblies. Aside from more traditional politicians, they included astrologers, transgender candidates, former models and the first female candidates in the tribal belt along the Afghan border.
Also running were dozens of candidates from Sunni sectarian groups, some with links to violent attacks on minority Shiites.
Mr. Khan's campaign, in particular, had a carnival atmosphere because it drew so many young urban residents into politics, many of them first-time voters. In the wealthier neighborhoods of Lahore on Saturday, supporters danced in the streets and waved giant flags bearing Mr. Khan's image.
But the sense of a vibrant, if flawed, democracy was tempered by Taliban attacks throughout the campaigning. The militant movement's ability to derail campaigning, particularly in the mountainous northwest, was taken as a signal that it has evolved beyond its nihilistic guerrilla roots and has become a powerful political insurgency bent on upending Western-style democracy in Pakistan.
Campaigning was further marred Thursday when Ali Haider Gilani, 27, the son of former Prime Minister Yousaf Raza Gilani, was shot and kidnapped by unidentified gunmen as he addressed a campaign event in the city of Multan, in southern Punjab Province.
Two guards who tried to protect Mr. Gilani were shot to death; the candidate was reportedly bleeding from a gunshot wound as he was dragged into a vehicle and driven away.
In a statement on Friday, the Taliban leader Hakimullah Mehsud ordered his commanders to attack the "infidel system" of democracy, warning that teams of suicide bombers would hit targets across the country.
At least 21 people were reported killed in attacks across Pakistan on Saturday, among them a gunfight and an attack on a polling place in the western province of Baluchistan and two explosions in the northwest, including in Peshawar, that injured several people. The deadly bombing in Karachi appeared to have been directed at a candidate from the Awami National Party, one of three secular-leaning parties that have borne the brunt of Taliban attacks that have killed at least 125 people in the last month.
The police established new checkpoints, and military helicopters patrolled the skies in Peshawar, the city that has been worst hit by militant violence over the years. Hospital workers were put on alert while billboards across the city asked residents to watch for suspicious activity.
After a slow start to voting, large numbers of voters emerged by midmorning, including many women. About 300 women in burqas stood in line outside the Lady Griffith High School, where policemen warned photographers not to take pictures of the women.
One of the women, Saba Iqbal, 35, a doctor, said she was going to vote for Mr. Khan's Pakistan Tehreek-e-Insaf party.
"I never voted before," she said, "but this time I want to be part of Imran Khan's change."
Mr. Zardari's Pakistan Peoples Party found itself badly overshadowed in the race after a lackluster and leaderless campaign.
The party developed a poor reputation for governance as the economy faltered in recent years and ministers failed to reverse crippling power shortages. Among the party kingpins who lost their seats on Saturday was Raja Pervez Ashraf, the previous prime minister, who suffered a crushing loss to a candidate from Mr. Sharif's party.
There were also signs of irregularities like those that tainted some past votes. At least one party, Jamaat-e-Islami, withdrew its candidates from Karachi and Hyderabad, protesting that the election was being rigged at various polling places.
Officials blamed the Muttahida Quami Movement party, which has traditionally dominated Karachi. A senior official with the Jamaat called for a peaceful strike on Monday to protest reported vote-rigging in the city.
Prominent officials of both Mr. Zardari's and Mr. Sharif's parties lodged similar accusations, saying they would reject some results in Karachi.
After polls closed, the Election Commission of Pakistan said that its staff had been threatened in the city, but that the voting had generally gone well.
Early on Sunday, The New York Times bureau chief in Pakistan, Declan Walsh, left the country three days after receiving a notice from the Interior Ministry that his visa had been canceled.
Mr. Walsh, who covered the elections on Saturday from Lahore, was accompanied to the airport by plainclothes intelligence agents, he said in a telephone interview. The men had closely paced him for hours.
He received the notice early Thursday morning from police officers outside his home in Islamabad. The order read in full: "It is informed that your visa is hereby canceled in view of your undesirable activities. You are therefore advised to leave the country within 72 hours."
No further details of any wrongdoing were given, and Pakistani officials did not respond to repeated requests for more information.
The Times's executive editor, Jill Abramson, filed a formal complaint with the Pakistani government on Friday, seeking Mr. Walsh's reinstatement and noting that his expulsion struck a troubling note for a country celebrating an important milestone as a democracy.
"The expulsion of an established journalist, on the day of the voting, contradicts that impression," she wrote.
Reporting was contributed by Salman Masood from Gujar Khan, Pakistan; Zia ur-Rehman from Karachi, Pakistan; Ihsanullah Tipu Mehsud from Peshawar, Pakistan; and Douglas Schorzman from New York.world
This article originally appeared in The New York Times.