Here's a little fairytale from Pakistan. Fourteen years ago a wise man ruled the country. He enjoyed the support of his people. But some of his treacherous generals thought he wasn't that smart. One night he was held at gunpoint, handcuffed, put in a dark dungeon, sentenced to life imprisonment. But then a little miracle happened; he, along with his family and servants, was put on a royal plane and exiled to Saudi Arabia, that fancy retirement home for the world's unwanted Muslim leaders.

Two days ago that same man stood on a balcony in Lahore, thanked Allah and said: Nawaz Sharif forgives them all.

But wait, if it was a real fairytale, Imran Khan would have won the election instead, right? Can't Pakistani voters tell between a world-famous, world cup-winning, charismatic leader and a mere politician who refers to himself in the third person?

Why didn't Imran Khan win?

Imran Khan in hospital after a fall at a campaign rally. Photograph: HO/AFP

Well he has, sort of. But not in the way he would have liked. Visiting foreign journalists have profiled Imran Khan more than they have profiled any living thing in this part of the world. If all the world's magazine editors were allowed to vote for Imran Khan he would be the prime minister of half the English-speaking world. If Imran Khan had contested in west London he would have won hands-down. But since this is Pakistan, he has won in Peshawar and two other cities. His party is set to form a government in Khyber Pakhtunkhwa, that north-western frontier province of Pakistan which Khan's profile writers never fail to remind us is the province that borders Afghanistan and the tribal areas that the world is so scared of. Or as some others never fail to remind the world: the land of the fierce pathans.

It's true that Khan ran a fierce, bloody-minded campaign, drawing huge crowds. When his campaign culminated in a televised tumble from a stage, during a public rally, the whole nation held its breath. Khan galvanised not only Pakistan's parasitical upper classes but also found support among the country's young men and women of all ages; basically the kind of people who use the words politics and politician as common insults. He inspired drawing-room revolutionaries to go out and stand in the blistering heat for hours on end to vote for him. For a few months he made politics hip in Pakistan. Partly, he was relying on votes from Pakistan's posh locales. He probably forgot that there was a slight problem there: not enough posh locales in Pakistan. There were kids who flew in from Chicago, from Birmingham to vote for him. Again, there are not enough Pakistani kids living and studying in Chicago and Birmingham. He appealed to the educated middle classes but Pakistan's main problem is that there aren't enough educated urban middle-class citizens in the country.

And the masses, it appears, were not really clamouring for a revolution but for electricity.

From the gossip columns of British tabloids to massive political rallies across Pakistan, Khan has been on a meaningful journey. In his campaign speeches, his blatantly Blairite message of New Pakistan did appeal to people but he really tested his supporters' attention span when he started to lecture them about how the Scandinavian welfare state model is borrowed from the early days of the Islamic empire in Arabia. Amateur historians have never fared well in Pakistani politics. Or anywhere else. Khan promised to turn Pakistan into Sweden, Norway or any one of those countries where everyone is blond and pays tax. His opponents promised Dubai – where everyone is either a bonded labourer or a property speculator and no one pays taxes – and won.

It's a bit of a fairytale that Khan, whose message was directed at educated urban voters, has found supporters in the north-western frontier province that profile writers must remind us is largely tribal and the front line of the world's war on terror. Khan has led a popular campaign against drone attacks. He has promised that he will shoot down drones, look Americans in the eye, sit down with the Taliban over a cup of qahwa and sort this mess out.

So we finally have someone who feels at home in Mayfair as well as Peshawar. He finally has the chance to rule Peshawar. Slight problem: as he speaks no Pashto, the language of the Pathans. But his first fight will be against American drones hovering in the sky. And drones speak no Pashto either. If Khan can win this match, he can challenge Nawaz Sharif in the next elections.

Is this Nawaz Sharif man for real?

Nawaz Sharif at a campaign rally in Liaquat Bagh, Pakistan. Photograph: T. MUGHAL/EPA

Hasn't he been tried before? Twice? It seems voters in the largest province of Pakistani Punjab just can't have enough of this guy. At every campaign stop, Sharif reminded his supporters of two of his biggest achievements: I built the motorway, I built the bomb. He did build Pakistan's first motorway. And despite several phone calls from the then American president Bill Clinton and other world leaders and offers of million of dollars in aid, Sharif did go ahead and order six nuclear explosions in response to India's five. And then he thought that now that both countries have the bomb he could go ahead and be friends with India. While he was making history hosting the Indian prime minister in the historic city of Lahore, his generals were busy elsewhere repeating history on the mountains of Kargil. In a misadventure typical of Pakistani generals, they occupied the abandoned posts and then pretended that these were mujahideen fighting India and not regular Pakistan army soldiers.

When India reacted with overwhelming force and a diplomatic offensive, Sharif pleaded ignorance and rushed off to Washington to bail out the army and his own government. President Clinton praised his diplomatic skills and the crisis was resolved briefly. When, months later he tried to fire his handpicked army chief General Pervez Musharraf, the architect of the Kargil fiasco, a bunch of army officers put their guns to Sharif's head. Handcuffed, jailed, sentenced to life imprisonment, in the end Sharif was saved by his powerful friends in Saudi Arabia. A royal jet flew him, his family and his servants to a palace in Saudi Arabia. An exile in Saudi Arabia for Muslim rulers is generally considered a permanent retirement home where you get closer to Allah and atone for past sins. Sharif must be the only politician in exile in Saudi Arabia who not only managed to survive this holy exile but in the process got a hair transplant and managed to hold on to his political base in Pakistan.

Many of his political opponents say that if Sharif wasn't from the dominant province Punjab, where most of the army elite comes from, if he didn't represent the trading and business classes of Punjab, he would still be begging forgiveness for his sins in Saudi. But he returned just before the last elections and has been behaving like a statesman. A very rich statesman.

It has yet to be proven whether eight years of exile in Saudi Arabia can make anyone wiser but it has never made anybody poorer. Sharif was rich before he got into politics, then he became fabulously rich. Even in exile the Saudis gave him a palace and, on his return, a fleet of bulletproof limousines. His campaign proved that poor people don't really vote for somebody who understands poverty, or wants to do anything about it. People have voted him in because he talks money, talks about spending money, talks about opening a bank on every village street and who doesn't like that? He has promised motorway connections and airports to towns so small that they still don't have a proper bus station. Poor people, who couldn't afford a bicycle at the time of the elections, like to be promised an airport. You never know when you might need it.

In his five years' rule in Punjab, Sharif's party has had one policy about the Pakistani Taliban who have been wreaking havoc in parts of Pakistan: please go and do your business elsewhere. And they have generally obliged. But now that he is set to rule all of Pakistan, what's he going to tell them?

Have we defeated the Taliban or sent them a friend request?

A voter displays her inked thumb after marking her ballot paper at a polling station in Karachi. Photograph: Athar Hussain/REUTERS

When Pakistan decided to throw itself an election party, the first ones to arrive were the Taliban. They weren't really interested in the party because they keep reminding us that elections are un-Islamic and a major sin on a par with educating girls. But they were interested in watching what party games were played and who got to play them. They decided that the three political parties that had ruled Pakistan for the past five years and taken a clear stand against the Taliban would be targeted. And the Taliban started their own campaign, targeting candidates and their supporters with bomb attacks and drive-by shootings. In one case, a candidate in Karachi was shot as he came out of a mosque. Along with his six-year-old son. The election campaign across Pakistan looked like this: some parties held huge rallies, in a carnival-type atmosphere with live tigers and massive music systems. Other candidates sneaked from one little corner meeting to another trying to remind people of their heroic stand against the Taliban. Many of the candidates were never seen in public. Many journalists refused to visit them because they were sitting targets. Bilawal Bhutto Zardari, the public face of the ruling People's party, could only deliver a couple of video messages from Dubai.

The other parties, the ones who were allowed to campaign freely, were grateful in their silence. When we look at the election results we must not forget that while the Pakistani Taliban didn't contest the elections as a political party, they did see themselves as kingmakers. In Pakistan's liberal media the Taliban are often described as brutes with an endless bloodlust. But by making allies and choosing partners they have demonstrated that they are at least as canny as the average campaigning politician.

But in the end the Taliban failed to deliver the kind of devastation they had promised. They managed to kill about 130 people in eight weeks. In the past they have achieved that kind of number in a single day. Also, 60% of Pakistanis who came out to vote seem to be politely disagreeing with the Taliban by saying that there is nothing un-Islamic about standing in a queue and stamping a ballot paper.

The Taliban's real success is that they bet on the winners. They promised not to attack Khan and Sharif's parties. And these parties will be in power. But the Taliban have never contested an election before. And they are soon to find out that politicians never keep the promises they make during the heat of the campaign.

Why does this election mean nothing for Farzana Majeed?

Pakistani voters line up at a polling station in Karachi. Photograph: Athar Hussain/Reuters

Three weeks before the elections, a 27-year-old biochemistry graduate stood outside Karachi Press club. Farzana Majeed and a couple of dozen young people carried pictures of Zakir Majeed, a literature student who was abducted by Pakistan's military intelligence four years ago and since then has become one of the hundreds of missing Baloch people, mostly young, political activists. Their mutilated, tortured bodies turn up on the roadside with sickening regularity. The Pakistani media, otherwise quite noisy about every subject under the sun, stay quiet. None of the political parties campaigning in recent elections uttered a word about Zakir Majeed or hundreds of other people languishing in military-run dungeons. Why? Because it's a security issue. A militant separatist movement in parts of Balochistan means that the rest of Pakistan sees it as an enemy. The protesters distributed pamphlets encouraging the fellow Balochs not to participate in the elections. The voter turn out in Baloch areas in Balochistan has been less than 10%. No political party in the country had the heart to go and ask Farzana Majeed or thousands of other families to vote. Farzana is a polite, articulate person but mention the word elections and she is likely to wave her missing brother's picture in front of you. And just like Pakistan's last political government, the new one also doesn't want to see this picture.

So what happens to the federation?

Watching the election results come in, in a teashop in Lahore. Photograph: Damir Sagolj/REUTERS

Who needs a federation when you can have so much more fun doing things your own way. So in the post-election Pakistan, Khan will rule the north and shoot down American drones while discussing Scandinavian social welfare models with the Taliban. Sharif will rule in Punjab and the centre, try to do business with India and build more motorways all the while looking over his shoulder for generals looking at him. In the south, Bhutto's decimated People's party will keep ruling and keep saying that folks up north are stealing its water, destroying its social welfare programmes and secular legacy. And, in Balochistan, Farzana Majeed will keep waving her missing brother's picture.

Do these bits add up to a country? They do, if you are sitting in Islamabad and showing off your nuclear weapons to the world or planning a motorway to central Asia. But if you are an old woman waiting for her 2,000-rupee welfare cheque or a student activist in a military dungeon waiting for your next interrogation session, you are not likely to dream of motorways and new airports.

Pakistan as a federation has gone through its first rite of passage: handover of power from one elected civilian setup to another. It took Pakistan 67 years to get here. Let us not forget that the reasons that caused this delay haven't disappeared.

Mohammed Hanif is BBC Urdu's special correspondent based in Karachi. He is the author of A Case of Exploding Mangoes and Our Lady of Alice Bhatti