Fourteen years after being overthrown by a military dictator and sent into exile, the former prime minister of Pakistan known to his supporters as the "Lion" has claimed victory in one of most dramatic general elections in the country's history.

Nawaz Sharif was on course to become the next prime minister of the troubled country facing multiple crises after it became clear he had secured a commanding lead in votes despite the challenge from Imran Khan, the former cricketer who had waged a ground-breaking campaign credited with galvanising an army of young people.

Although the final result was still hours away, the country's television networks projected Sharif heading for an ultimately tally of around 110 parliamentary seats, comfortably on course to secure the parliamentary majority he said he desperately needed to fix Pakistan's ailing economy.

Khan's Pakistan Tehreek-e-Insaf (PTI) appeared to be headed for a final tally of around 35 seats, in line with the expectations of many analysts, but far below the stratospheric expectations of his followers who had been repeatedly promised a "tsunami" that would sweep away the country's established parties.

Meanwhile, the Pakistan Peoples party (PPP), which is wildly unpopular after five years in government, was battered at the polls, and was set to secure around 35 seats, most of them in its traditional rural strongholds in Sindh province.

Sharif emerged on to the balcony of the Lahore headquarters of his Pakistan Muslim League-Nawaz party (PML-N) well before midnight to talk to his delirious supporters, who cheered and waved the green flags of the party.

He said he was prepared to work with all other parties to try to fix the country's many problems. "We want to change the destiny of the country and fulfil the promises we made to the nation during the election campaign," he said.

While PML-N supporters erupted in noisy celebration across Lahore many PTI supporters reacted angrily to their disappointing result, with some beating cricket bats – the symbol of the party – on one of the city's main boulevards.

One gain for Khan was the leading position the party gained in Khyber Pakhtunkhwa, the province close to the troubled frontier with Afghanistan, where the PTI could become a leading partner in a coalition government.

However, given the province's acute problems and struggle with Taliban militancy, it might prove a mixed blessing for a party that has never come close to governing anything in its history.

Merely having an election is regarded as a major achievement for Pakistan and a huge milestone in its efforts to strengthen democracy. It is the first time in its history that one elected government has handed power to another. However, despite a strong turnout, which in the critical battleground of Punjab was described as "huge" by election officials, the day was also marred by violence that killed 22 people and vote-rigging in the country's biggest city.

Politicians began crying foul even before counting began in the 70,000 polling stations throughout the huge country, particularly in the volatile port city of Karachi where a number of parties announced they would withdraw from the contest in protest.

The PTI in particular complained of blatant efforts to intimidate voters in Defence and Clifton, two affluent neighbourhoods where Khan has a devoted following. There were reports of people being threatened as they attempted to go to vote and one case of gunmen opening fire at a polling station.

Sana Bilal, a 28-year-old PTI supporter, said her polling station located in a school in the Defence neighbourhood never even opened. "There were people who have been waiting outside since 8am [the official start of polling nationwide]," she said. "It was a sea of PTI supporters. Everyone is just really angry that we've been deprived of our right to vote."

The Muttahida Quami Movement, the dominant political party in the city, was immediately blamed for attempting to suppress voting in an attempt to prevent the PTI eating into its territory.

Pakistan had been bracing for violence after the Taliban vowed to use suicide bombers to disrupt a democratic process the extremist movement regards as un-Islamic. The government deployed 600,000 security personnel in an effort to protect the vote.

Many voters took the threat seriously, circulating election day tips on social media that included remaining watchful for any abandoned packages.

Some 22 people were killed nationwide in a series of attacks, some by bombs placed near polling stations in the restive north-west of the country.