Pakistan's civilian governments have often been at loggerheads with the judiciary and military, who are frequently accused of meddling in a country with a history of coups. Here are the main players in the country's politics.

Nawaz Sharif - Prime minister

Image caption Nawaz Sharif - back from years in the wilderness

Nawaz Sharif is back in office for a record-breaking third term as prime minster after a remarkable political comeback. Toppled by the military in 1999, jailed and then exiled, he returned to the country for elections in 2008 before patiently biding his time in opposition.

His Pakistan Muslim League-N (PML-N) remains the main political force in Punjab, the most populous province of Pakistan, having fought off a challenge from the revitalised party of Imran Khan. Mr Sharif was tipped to win, but the margin of his victory has surprised many. No stranger to corruption allegations himself, Mr Sharif has ridden a wave of anger against the perceived corruption and inefficiency of the outgoing PPP government.

A wealthy industrialist, he is seen as pro-business - one of the most pressing tasks he now faces will be sorting out an economy in crisis. Neighbouring countries have welcomed his win - it means a more stable government in Pakistan - although many Afghans in particular still view him with suspicion.

Mr Sharif will have to deal with Pakistan's security crisis and efforts to end war over the border in Afghanistan. He denies being soft on militants - but critics say he tolerates religious extremism. Pakistanis may remember how unpopular he was before the coup, when many saw him as power-hungry. Voters hope he is now older and wiser.

Correspondents say now he is in power Mr Sharif may have to accept the army view that there is no option but to fight militants who attack domestic targets.

Asif Ali Zardari - opposition leader

Image caption Mr Zardari presided over an increasingly unpopular government

One of Pakistan's most controversial political figures, Asif Ali Zardari was often considered something of an accidental president. He came to power in September 2008 on a wave of public sympathy following the assassination of his wife, former Prime Minister Benazir Bhutto. But his term in office - which ended in September 2013 - was one of unrelenting political and social turmoil, growing instability and persistent allegations of corruption and economic mismanagement.

His Pakistan People's Party (PPP) was trounced in the 11 May vote. Under Mr Zardari's leadership Pakistan's relationship with Washington deteriorated, with the US questioning whether Islamabad was doing enough to tackle militancy.

The career of Mr Zardari - nicknamed Mr Ten Percent - has been dogged by corruption allegations. The Supreme Court has been pushing to reopen a corruption case for which he has already spent eight years in prison. He is increasingly unpopular with the Pakistani public. Health problems have seen him travel abroad for treatment at crucial moments.

Another test for his leadership came in late 2011 with the leaking of the memo asking for US help to avert a possible coup, following the killing of Osama Bin Laden earlier that year. Mr Zardari denied involvement in the "memogate" scandal, and appears to have escaped possible impeachment at least until the end of his time in office.

Imran Khan - opposition leader

Image caption Imran Khan's popular support was not enough to win the vote

The former international cricketer has been on the political scene for years, but struggled to make any significant gains until now. He won his Pakistan Tehreek-e-Insaf (Movement for Justice) party's only seat in 2002, and he boycotted the 2008 elections.

In the last couple of years, Mr Khan has tried to bolster his popularity by riding a wave of disillusionment, particularly among the urban middle class and young voters. He sought to capitalise on anger against US drone strikes in Pakistan's tribal areas. He also succeeded in winning the support of some politicians who had grown disgruntled with their own parties, which helped counter accusations of political inexperience.

One of the main promises of Mr Khan's change agenda was to sweep away the rampant corruption plaguing Pakistani politics, in part by calling for an end to foreign aid. But correspondents cautioned he had some way to go to turn popular support into electoral gains. He is, however, believed to be popular with the military.

In the end all the rallies and Mr Khan's high profile failed to produce the "political tsunami" he predicted on 11 May. However, his party narrowly missed out coming second largest nationally and leads the government in Khyber Pakhtunkhwa province, which is plagued by conflict between security forces and militants.

Raheel Sharif - army chief

Image caption Raheeel Sharif has a reputation as a deep military thinker

Gen Sharif is seen by many as a straight-talking, professional soldier with no political ambitions, a factor which is of some importance in Pakistan, given its history of military coups.

The general is not related to Prime Minister Nawaz Sharif who appointed him in November 2013.

He played a key role from 2007 in formulating a re-think of military strategy. He argued that the focus of the military's attention ought to be directed principally against Taliban militants rather towards India.

Correspondents say that he brings a sound mix of academic and field experience to the job, which many consider to be the most powerful in Pakistan.

While some say that his political and military connections got him the top job, few have doubted his military credentials - he comes from a family of soldiers.

Zaheerul Islam - intelligence chief

Lt Gen Zaheerul Islam replaced Ahmad Shuja Pasha as head of the Inter-Services Intelligence (ISI) spy agency in 2012.

He took up the post after a tumultuous year in which the agency was accused by the US of backing militants across the border in Afghanistan.

Pakistan has consistently denied such claims but relations with the US have been tense.

Upon assuming office, he had to deal with the fallout of the Bin Laden killing - in which some in the US accused Pakistan's establishment, and especially the ISI, of at best incompetence and at worst complicity after the al-Qaeda chief was found to have lived in the country for a number of years.

Lt Gen Islam was born into a military family and has also served as deputy head of the ISI.

Many believe the ISI head could be an important figure in any future Pakistani peace negotiations with the Afghan Taliban.

Pervez Musharraf - former military leader

Image caption Gen Musharraf may rue going back to Pakistan

Pakistan's last military leader dramatically returned in March 2013 to compete in elections, ending four years of self-imposed exile and defying death threats.

But it seems it was a strategic mistake to do so, because he now faces trials in a series of court cases, including one for treason.

In one case, he is accused of failing to provide adequate security for former Prime Minister Benazir Bhutto, which contributed to her assassination in 2007.

As president from 2001 to 2008, Pervez Musharraf was one of Pakistan's longest-serving rulers. But he was barred from standing in 2013 and his All Pakistan Muslim League (APML) performed as badly as many predicted it would.

The military is thought to be viewing his predicament with a degree of concern, aware that his case could set a precedent.