LAHORE, Pakistan — After nearly reaching the end of a long and contentious vote-counting process, Pakistan’s election commission said on Friday that the party of Imran Khan, the former cricket star, won the most seats in the National Assembly, setting him up to be the country’s next prime minister.

Although Mr. Khan’s party, the Pakistan Movement for Justice, did not win an outright majority of the seats, analysts say they believe he will be able to form a coalition government, and some of his political rivals, who had been complaining about how the national election was conducted, began to grudgingly accept his victory.

Mr. Khan’s party swept most of the country, performing strongly in urban areas and so far winning 115 parliamentary seats, compared with 64 for the second-place party, known as the P.M.L.-N.

Mr. Khan, who has been on a quest for higher office for the past 20 years, was the favorite candidate of Pakistan’s powerful military. Human rights groups and many analysts have said that in the months before the election, military and intelligence officials threatened and blackmailed politicians in rival parties to defect to Mr. Khan’s side, clearing a path for his victory. Mr. Khan has denied this.