Nawaz Sharif signs on an attendance register at the National Assembly (lower house of the parliament) in Islamabad. Sharif was elected as prime minister for the third time by the assembly on June 5, 2013. (PAKISTAN MUSLIM LEAGUE-N/via EPA)

ISLAMABAD — Pakistan’s National Assembly formally chose Nawaz Sharif as prime minister on Wednesday, capping a celebratory and historic transfer of power in a country roiled by decades of turmoil and facing immediate economic challenges.

Sharif, 63, leader of the Pakistan Muslim League-N, was elected with an overwhelming majority, capturing 244 of the 294 ballots cast in the lower house of Parliament. The vote, held in the grand Parliament House in the capital, represented a rare moment of unity in Pakistan, with even members of several rival parties saying they want to give Sharif space to form a strong government.

Sharif hailed it as a defining occasion in the country’s 65-year-old history, marking the first peaceful transition from one democratically elected government to another.

“We have no other option except for democracy,” said Sharif, who served two terms as prime minister in the 1990s and was ousted in a coup, jailed and then exiled to Saudi Arabia until 2007. “We should strengthen the democracy for the better prospects of Pakistan, and we should close all doors to dictatorship.”

Sharif vowed to swiftly address chronic energy shortages that leave residents of the nuclear-armed nation without electricity and air conditioning in sweltering summer heat for up to 15 hours a day.

He also warned the Obama administration to halt drone strikes on Pakistani soil, reflecting the unease here about the attacks aimed at Islamist militants and Taliban leaders.

Despite past concerns about Sharif’s extravagant lifestyle and reputation for arrogance when he held the premiership — from 1990 to 1993 and from 1997 until he was ousted in 1999 — his election was generally welcomed across Pakistan, which faces numerous challenges, including high unemployment, fuel shortages and a lack of clean drinking water.

News anchors were giddy as they covered the voting process, and the English-language news channel repeatedly flashed the word “democracy” across the screen.

“We believe, and hope, we can now truly join the community of nations,” said Khalil ur-Reham Ramday, a former Pakistani Supreme Court justice. “It will not be an easy job, but at the same time, you have to take that first step. . . . We believe the people will start seeing the benefits of democracy.”

In one of Islamabad’s largest shopping districts, residents said they have high hopes that Sharif can succeed, although some said it won’t be easy for him to overcome his record.

“We are very happy,” Mahood James, a 30-year-old taxi driver, said of Sharif’s return to power, as he sat on a blanket with a half-dozen other drivers waiting for a fare. “But he needs to look back and not repeat old habits.”

The drivers were unanimous in supporting Sharif’s pledge to stop U.S. drone strikes, saying they view the issue as a key test of his muscle on the international stage.

In the northwestern city of Peshawar, Israr Khan Yousafzai, 40, a trader and graduate of the city’s agricultural university, voiced confidence as he dealt with customers. “Unlike previous governments, I am optimistic that the Nawaz Sharif-led new government will deliver on the economic and policy sides,” he said.

In a cycle of political upheaval since the country split from India in 1947, Pakistan’s governments have endured assassinations, imprisonments and military-backed coups, including the one former army chief Gen. Pervez Musharraf led against Sharif in 1999.

But the administration of President Asif Ali Zardari, who was elected a year after his wife, former prime minister Benazir Bhutto, was assassinated in 2007, survived five years, even though it appeared close to collapse many times.

Zardari, a member of the Pakistan People’s Party, struggled with an unruly governing majority, unstable economy, several high-profile scandals, a series of spectacular terrorist attacks and tension between him and the Pakistani army over Washington-backed efforts to combat the Taliban and other militants.

Yet Zardari oversaw constitutional reforms that gave more authority to local provinces while also strengthening the powers of the prime minister. The change set up the May election, which saw a robust turnout, despite a wave of bombings and assassinations by militants who denounce democratic elections as a violation of Islamic law.

Observers say Sharif has perhaps six months to a year to show a wary public that he is equipped to revive the economy, which is suffering from, among other things, historically low foreign investment.

Michael Kugelman of the Washington-based Woodrow Wilson International Center for Scholars wrote in March that “time is running out” for Pakistan to address its energy crisis and avoid “catastrophic” and destabilizing consequences.

Sharif did not offer specifics during his address on how he would try to resolve the crisis, but he said he has a team of advisers working on it around the clock. “My government is ready to face these grave issues,” he said.

Shuja Nawaz, director of the Atlantic Council’s South Asia Center, said Sharif’s first priority will be to get “some breathing room” from the public by addressing the energy shortage, so he can then tackle the more acute economic challenges.

“He has to focus on energy and find short-term measures, but he also has to bring people on board to effect the longer measures that need to be made to ensure growth,” Nawaz said.

Sharif faces the equally delicate task of managing relations not only with rival India, another nuclear-armed nation, but also with the United States as it seeks an ally in its efforts to assure stability in neighboring Afghanistan as NATO troops withdraw.

Noting his experience on the international stage, U.S. officials have expressed confidence that they can work with Sharif, even though he has expressed a desire to hold peace talks with the Pakistani Taliban. But his influence is likely to be tempered by the wishes of Pakistan’s military, which remains a powerful presence.

Still, five years after military rule ended, few observers expect that Sharif will have to worry about another coup, noting that the military is showing signs of greater openness to the democratic process.

“The army has their hands full with the Taliban and various other operations,” said Ayaz Amir, a former member of Parliament. “So, Sharif has everything going for him except the magnitudes of the problems, and the problems are huge.”

Haq Nawaz Khan in Peshawar contributed to this report.

Tim Craig is The Post’s bureau chief in Pakistan. He has also covered conflicts in Iraq, Afghanistan and within the District of Columbia government.

Comments our editors find particularly useful or relevant are displayed in Top Comments, as are comments by users with these badges: . Replies to those posts appear here, as well as posts by staff writers.

To pause and restart automatic updates, click "Live" or "Paused". If paused, you'll be notified of the number of additional comments that have come in.

Comments our editors find particularly useful or relevant are displayed in Top Comments, as are comments by users with these badges: . Replies to those posts appear here, as well as posts by staff writers.