Kenyan president Mwai Kibaki appeared to be heading for a humiliating election defeat last night as voters signalled their discontent with his government by evicting most of the powerful political old guard from office.

Local television stations reported that, with more than half the votes counted by midnight local time, opposition leader and former political prisoner Raila Odinga had won 3.6m votes, a 51% share, compared with 42% for Kibaki, on 2.9m votes. Odinga's Orange Democratic Movement was also heading for an overwhelming victory over Kibaki's Party of National Unity.

The election commission of Kenya trailed far behind the media in releasing results, although it also showed Odinga comfortably in the lead. Though it was still theoretically possible for Kibaki, 76, to reverse the deficit, analysts said it would take a minor miracle.

If he does not, it will be the first time a Kenyan president has lost through the ballot box, and will represent a clean break from the political dynasty that has governed the country since independence.

It is a remarkable reversal of fortunes for a man who enjoyed huge support when he was elected in 2002, ending 24 years of autocratic rule by Daniel arap Moi. Although Kibaki helped to improve the economy and civil service, his decision to remain loyal to political veterans - many with dubious records, or from his own Kikuyu ethnic group - rather than bring in new blood, has counted against him.

At least 16 of Kibaki's 32-member cabinet members were confirmed to have lost their seats, including his vice-president Moody Awori, 80, and the defence and roads ministers, both in their late 70s.

"This was an unprecedented revolt from the ground against the corruption of parliament and against the old political class," said Mutahi Ngunyi, a political scientist in Nairobi. "It's a huge vote of no confidence in the government, and allows a new generation a chance at leadership."

A part of the winning coalition five years ago, Odinga left the government in 2005, angered by Kibaki's failure to honour a power-sharing agreement and his attempt to push through an unpopular constitution. Odinga has been campaigning ever since, accusing Kibaki of failing in his election pledges to end high-level corruption and to create 500,000 jobs a year. Under-40 voters in particular, many of whom remain unemployed, bought into Odinga's message that he was the man to bring about the change they had voted for in 2002.

In an attempt to close the gap, Kibaki, who had already drawn Uhuru Kenyatta, son of Kenya's first president, Jomo Kenyatta, into his party, enlisted the help of the second president, Moi.

It was a damaging misjudgment of the public mood. Even in Moi's traditional stronghold of Rift Valley province his legacy of corruption and misrule overshadowed his supposed elder statesman role, and he was booed at rallies.

Three sons of Moi who stood for parliament lost, including Gideon Moi, in the seat he took over from his father. In August he featured prominently in a leaked report commissioned by the government from the risk consultancy Kroll, which aimed to trace the assets of people suspected of looting state coffers. The other notable MP in the Kroll report, Nicholas Biwott, also lost his seat.

Others voted out included David Mwiraria and Chris Murungaru, who were forced to step down as ministers in 2006 after being accused of complicity in a huge corruption scandal known as Anglo Leasing, which involved bogus companies, some registered in the UK. They are both banned from visiting Britain.

International observers praised the organisation and conduct of the poll, although there was violence at several polling stations during counting yesterday.