Pakistani legislators have elected former cricket star and long-time politician Imran Khan as the country’s next prime minister, in a step towards the country’s third civilian transfer of power.

He secured 176 votes in a vote at the National Assembly, defeating opposition’s candidate Shahbaz Sharif of the Pakistan Muslim League party, who got 96 votes.

Mr Khan’s populist Tehrik-e-Insaf party won the most seats in July 25 elections but fell short of a majority in the 342-seat house.

He will be sworn in as prime minister on Saturday, with a five-year mandate.

Supporters of Imran Khan celebrate outside the National AssemblySupporters of Imran Khan celebrate outside the National Assembly (BK Bangash/AP)

His supporters celebrated across the country when speaker Asad Qaiser announced result.

In his first speech to lawmakers as premier, Mr Khan dismissed allegations of election fraud and vowed to enforce “ruthless accountability” to combat corruption.

Mr Khan is a former Oxford graduate and formed his party in 1996 in the eastern city of Lahore, vowing to forge a corruption-free “new Pakistan” with justice for all without discrimination.

He challenged the parties of Mr Sharif and former president Asif Ali Zardari over the past two decades, but was not able to make a strong showing until 2013 when his became the third-largest party in the lower house of parliament.

Shahbaz Sharif arrives at the National Assembly in IslamabadShahbaz Sharif arrives at the National Assembly in Islamabad (BK Bangash/AP)

Mr Sharif is the younger brother of former prime minister Nawaz Sharif, who is serving a 10-year jail term in a corruption case.

The elder Sharif, who served as premier three times, was disqualified last year by the Supreme Court for concealing assets abroad.

Later, an anti-corruption tribunal convicted and sentenced him to 10 years in jail over the family’s purchase of luxury apartments in London.

Mr Khan has in recent years led widespread protest rallies against Sharif’s government after the 2013 elections, alleging ballot rigging.

Addressing lawmakers, the younger Sharif insisted that last month’s elections were manipulated in Mr Khan’s favour.

He and his supporters chanted slogans against Mr Khan during his speech, claiming he was brought to power by the military.

Bilawal Bhutto Zardari, head of opposition Pakistan People’s Party, in his first speech as lawmaker, also claimed that the national elections were rigged, “which was a shame”.

Mr Khan insisted that neither Mr Sharif nor any other person would politically strong-arm him through protests.

The new premier acquired a reputation as a playboy during his cricketing years but has since embraced conservative Islam after entering politics.

Imran Khan married Jemima GoldsmithImran Khan married Jemima Goldsmith in 1996 (Sean Dempsey/PA)

He has married three times. His first wife was wealthy British heiress Jemima Goldsmith, who he married in 1996. Their two sons live with Ms Goldsmith, who has publicly supported Mr Khan’s political ambitions and praised his skill as a leader, even after their divorce in 2004.

His married his second wife, British journalist Rehman Khan, in 2015. They divorced within a year.

Earlier this year, he married his spiritual adviser, Bushra Maneka.

In his 2011 book Pakistan: A Personal History, Mr Khan noted his ambition to come to power when his Islamic nation was ready to hold free and fair elections.

Aside from tackling corruption, and a vision to introduce “surgical reforms” in governance, one of the key challenges facing him is how to improve ties with the US.

He was elected a day after the Foreign Ministry confirmed the suspension of a US military training programme for Pakistani soldiers. The announcement underscores persistent tensions between the two allies in the war on terror.

Mr Khan had previously described President Donald Trump’s policies towards Pakistan as “flawed”.