Updated

Pakistan's former prime minister Nawaz Sharif looked set Sunday to return to power for a third term, with an overwhelming election tally that just weeks ago seemed out of reach for a man who had been ousted by a coup and was exiled abroad before clawing his way back as an opposition leader.

As unofficial returns continued to roll in Sunday, state TV estimates put Sharif close to the majority needed to govern outright. Even if he falls short of that threshold, independent candidates almost certain to swing to his favor would give Sharif's Pakistan Muslim League-N a ruling majority.

The margin of victory over the closest competitors -- a party headed by former cricket star Imran Khan and the outgoing Pakistan People's Party -- gave Sharif's party a clear mandate to guide the country of 180 million over the next five years.

"It's clear that Nawaz Sharif will form the federal government," said political analyst Mehdi Hasan.

Supporters danced in the streets overnight in his hometown of Lahore, Pakistan's second largest city and the provincial capital of Punjab province.

Violence marred the vote in the southern port city of Karachi, the northwest and in the southwestern province of Baluchistan. At least 29 people died in election-related attacks, but people still came out in droves. Election officials said the turnout was close to 60 percent, easily eclipsing the 44 percent of voters who came to the polls in 2008.

Sharif fended off a strong challenge from Khan's Pakistan Tehreek-e-Insaf party. Khan, who led the country to victory in the 1992 cricket World Cup, had tapped into the frustrations of many Pakistani youths fed up with the country's traditional politicians.

But in the end, it was Sharif and the PML-N who emerged as the clear winners. The seat projections indicated that his party would have a much stronger grip on power than its predecessor.

Supporters in Lahore said they hoped that would bring progress after the outgoing Pakistan People's Party government, which much of the country saw as only focused on its survival.

"It will bring stability in our country," said Fayaz Ranjha. "We have voted for them, now it is their turn to take steps to end our miseries."

The 63-year-old Sharif served as prime minister twice during the nineties and oversaw Pakistan's first nuclear weapons test, but was ousted in a coup in 1999 by former chief of the army, Gen. Pervez Musharraf.

Sharif went into exile in Saudi Arabia and only returned to Pakistan in 2007. Even then, he was forced to sit on the sidelines as his party contested parliamentary elections after a court disqualified him from running. He had a prior criminal conviction for terrorism and hijacking stemming from Musharraf's coup -- Sharif was accused at the time of denying the general's plane permission to land.

The Supreme Court overturned the conviction in 2009.

Over the last five years, Sharif put steady pressure on the PPP-led government, but, wary of army interference, never enough to threaten its hold on power. This attitude helped enable parliament to complete its term and transfer power in democratic elections for the first time since the country was founded in 1947.

Sharif now faces the monumental task of governing a country with rising inflation, rolling blackouts, and a powerful Taliban insurgency.

The PML-N will also inherit a rocky relationship with neighboring Afghanistan. Afghan President Hamid Karzai on Sunday praised Pakistan for having carried out general elections despite the violence.

While promising "full cooperation" with Pakistan's new government, Karzai alluded to the often hostile relationship between the two countries and his suspicions that Islamabad has in the past aided insurgents and contributed to Afghanistan's instability.

"We hope that the new elected government provides the ground for peace and brotherhood with Afghanistan, and to sincerely cooperate in rooting out terrorist sanctuaries," he said.

Analysts say Sharif is likely to want to assert a stronger influence over the Pakistani military than President Asif Ali Zardari or his PPP government have, which could cause tension. Sharif's relationship with the army will be watched closely for any sign of a rift similar to the one in 1999.

He'll also be observed to see what moves -- if any -- he takes to reign in militants and deal with religious extremists who have threatened the country's stability for years. Critics say the PML-N has tolerated extremist groups in the party's stronghold of Punjab province.

"They think they cannot afford to stoke unnecessary trouble for them by cracking down on people or groups who are extremists or terrorists," said Imtiaz Gul, director of the Islamabad-based Center for Research and Security Studies. "I don't think that these guys have enough understanding of the risk."

In relations with the United States, Sharif is expected to be more nationalistic and protective of state sovereignty than the outgoing government. He defied U.S. opposition to Pakistan's nuclear test in 1998 and has criticized the Afghan conflict as "America's War."

But the often testy ties between Washington and Islamabad are not expected to change radically since Pakistan's powerful army still plays a dominant role on foreign policy issues and is eager not to lose the hundreds of millions of dollars in U.S. military aid.

Washington will also play a deciding role in any bailout package that Pakistan will almost certainly need from the International Monetary Fund to prop up its ailing economy.

Sharif will face a host of economic problems. Pakistanis suffer through power outages that can last 18 hours a day and extensive gas outages in the winter. Inflation has risen sharply, and foreign investment dropped.

The new government will need to address how to increase the country's tax revenue, reduce fuel subsidies and restructure ailing state-run industries, said Ashfaq Ahmed, dean of the business school of the Islamabad-based National University of Sciences and Technology.

Sharif has a track record of trying to improve ties with Pakistan's arch rival, neighboring India, and is expected to continue to work toward that while in power. A thaw in relations could improve Pakistan's economy by opening up trade with India and -- in the long-term -- by reducing the need for defense spending.

The election in many ways highlighted ethnic divisions that have plagued Pakistan since its inception in 1947.

The PML-N drew most of its strength from Punjab province. The Punjabis are the country's largest ethnic group, and the province has sometimes alienated the rest of Pakistan by its dominance in government and the military.

The PPP, a party that used to draw on nationwide support, was relegated to Sindhi-speaking areas in southern Pakistan. Khan, an ethnic Pashtun, dominated in the mostly Pashtun areas of northwestern Pakistan.

Sharif will have to work hard to show that he represents all of Pakistan, not just Punjab province.

"If there is a heavy mandate from Punjab particularly the smaller provinces will complain that Punjab is exploiting him," said the political analyst Hasan.

Khan's party was in close competition with the PPP over who would form the second-largest bloc in parliament. That's a significant gain for a party that had only ever won one national assembly seat previously.