ISLAMABAD, Pakistan — Imran Khan, the charismatic cricketer-turned-politician, was elected prime minister on Friday in an acrimonious vote in the lower house of Parliament that was punctuated by partisan shouting.
“Those people who have looted the country, I promise that they will be brought to justice,” Mr. Khan said in a brief speech after the vote, repeating an anti-corruption campaign theme and offering little conciliation to his adversaries.
Surrounded by loyalists as the opposition’s sloganeering grew louder, he added: “No one has ever been able to blackmail me before. I tell you please go ahead and protest and hit the streets.”
Mr. Khan, the leader of the Pakistan Tehreek-e-Insaf party, received 176 votes on Friday while his opponent, Muhammad Shehbaz Sharif of the Pakistan Muslim League-Nawaz, got 96. He is scheduled to take the oath of office on Saturday.
The hall of Parliament erupted as soon as Asad Qaiser, the speaker, announced the vote count. Mr. Khan’s supporters exalted “Prime Minister Imran Khan,” while his opponents stood up and roared “Vote thief not acceptable,” alluding to allegations of rigging in the July 25 general election.
Mr. Khan sat smiling after his victory, holding a prayer bead in one hand and occasionally shaking his head in disapproval of the opposition ruckus.
Mr. Khan’s party, known as PTI, holds 151 seats in the parliament. Pakistan Muslim League-Nawaz, the former governing party, has 81 seats. The Pakistan Peoples Party, the third-largest party, has 54 but abstained from Friday’s voting.
The uproar in Parliament may be a prelude to the difficult path ahead for the new prime minister. The large number of opposition members in the two chambers, the National Assembly and the Senate, will make legislating a tough bargain.
PTI’s rise has marked a stunning turnaround in Mr. Khan’s political fortunes. He emerged as a serious contender only in 2011 when he began attracting large crowds at rallies.
For a long time, he was considered a nobody in the political landscape, and people often mocked his ambition of becoming prime minister. In 2002, his party had just one seat in Parliament. He boycotted the 2008 elections, and in 2013 managed to increase his party’s share to 35.
But he has a history of doing the improbable. Pakistan’s cricket team emerged as one of the best in the world under his leadership and won the World Cup in 1992. Mr. Khan also built the country’s first cancer hospital that treats poor patients for free after raising money through charity.
Mr. Khan has no real governing experience and will face an enormous task in steering the country out of its economic and political challenges.
During the election campaign, Mr. Khan vowed to end corruption and official malfeasance. A broad section of the country’s population, especially the young and educated middle class, has been enamored of his promise of change and reform.
But the elections on July 25 were also marred by allegations of the powerful military’s meddling and irregularities in voting.
The military has denied tilting the election in Mr. Khan’s favor, but the party formerly in power, Pakistan Muslim League-Nawaz, has leveled loud accusations that intelligence agencies forced its members to defect to Mr. Khan’s party or run as independents.
The corruption conviction and jailing of Nawaz Sharif, the former prime minister, just before the elections also helped Mr. Khan’s ascent.
In many ways, Mr. Khan was responsible for the downfall of Mr. Sharif, a towering figure in the country’s politics who has served as prime minister for three times but never managed to complete a full term.
Mr. Khan led large-scale street protests against Mr. Sharif and helped to advance Supreme Court cases that led to his ouster in July 2017.
Muhammad Shehbaz Sharif, the younger brother of Mr. Sharif, is now running their political party but analysts here say he cannot match the elder Sharif’s leadership style.