According to the private TV network Geo, Sharif's center-right PML-N was leading the race with 126 seats and Pakistan Tehreek-e-Insaf (PTI) had 34.
With just 32 seats so far, the Bhutto clan's Pakistan People's Party (PPP) -- which led the outgoing government with 125 seats in the old national assembly -- was relegated to third place.
Flanked by his brother Shahbaz and his daughter Maryam, Sharif gave a victory speech late Saturday to hundreds of jubilant supporters at PML-N headquarters in Lahore.
"We should thank Allah that he has given PML-N another chance to serve you and Pakistan," he said, after nearly 60 percent of the 86 million electorate turned out to vote despite polling day attacks by the Taliban that left 24 dead.
Sharif, who has vowed a pro-business agenda to revive Pakistan's crippled economy, struck a conciliatory tone following Khan's high-voltage campaign.
"I appeal for all parties to come to the table and sit with me and solve the country's problems," Sharif said.
The election was fought over the tanking economy, an appalling energy crisis that causes power cuts of up to 20 hours a day, the alliance in the US-led "war on terror" and chronic corruption.
Prime minister twice before in the 1990s, Sharif's third term will begin only after he brokers a deal with political rivals to form a coalition.
Political analysts said the result would most likely be a hung parliament in which the PML-N would have to team up with its former opponents from the outgoing government led by the PPP.
Besides the 342-member national assembly, voters also elected four provincial assemblies in Khyber Pakhtunkhwa, Punjab, Sindh and Baluchistan.
Khyber Pakhtunkhwa is a hotbed of militancy. Both Sharif and Khan have vowed to talk to the Taliban and have tapped into the Pakistani public's deep hatred of the US drone attacks on the country.