Daniel arap Moi, who has died aged 95, was born into a poor peasant family in the Rift Valley in British colonial Kenya, and rose to become one of post-independence Africa’s longest surviving leaders. But his ignominious departure from power at the end of 2002, after 24 years as president, when the candidate he groomed to succeed him was roundly defeated, told the real story of his years in power.

It was a story of stability maintained by ruthless manipulation of the ethnic card and of his opponents’ weaknesses, and by the refinement of a culture of corruption and impunity inherited from his predecessor, Jomo Kenyatta, Kenya’s first president.

Moi, whose given name at birth was Toroitich, spent his early years in Kurieng’wo village, in Baringo, western Kenya, with his brother, tending the few sheep and goats left by his father, Kimoi arap Chebii, a herdsman, who died when Moi was four. His paternal uncle sent him to a Protestant missionary primary school where he took the Christian name of Daniel. He went on to another missionary school for his secondary education, before joining the government school at Kapsabet, 100 miles from home. Every term he would walk to and from school.

Moi became a successful teacher, and then assistant principal of a teacher training college, before moving into colonial politics. In October 1955 he became a member of the Legislative Council of Kenya (Legco) – one of five Africans nominated by the British colonial government.

At independence in 1963 he became minister of home affairs, and three years later also vice president to Kenyatta. Moi, as a member of the small Kalenjin ethnic group, was a convenient outsider – and one who owed everything to the president – for Kenyatta to rely on, as his own group of Kikuyu politicians feuded for dominance in his administration.

Those were years of stability for the country, and Kenya prospered with investment and loans – the fruit of Kenyatta’s unwavering pro-western policies that included allowing British troops to be stationed in Kenya. The international community turned a blind eye to the flagrant corruption at the top of Kenyan politics and the political murders that removed the less pliant opposition figures.

Daniel arap Moi, surrounded by heavy security, campaigning in Nairobi during the December 1997 elections. Photograph: Corinne Dufka/Reuters

When Kenyatta died in August 1978, Moi as vice president became constitutionally president for an interim period of 90 days. All eyes were on the bitter fight for the succession between two competing groups of Kikuyu political heavyweights within the ruling political party, the Kenya African National Union (Kanu). Moi, seen as colourless, and lacking an ethnic base of any importance, was not considered as even a possible contender for the top job.

Even when Moi was elected, as a unifier, his presidency was not expected to last, so dominant were the other competing candidates. But they self-destructed in power struggles with each other, and Moi, with his philosophy of “Nyayo” (peace, love and unity), was initially accepted by Kenyans. They believed he could give the country a chance of overcoming the tribalism that had so marked the corruption and influence-peddling of the previous administration.

However, intolerance and exclusion soon became the hallmarks of Moi’s regime, and in June 1982 the constitution was amended to make Kanu the only legal political party. A coup attempt led by the air force followed in August, and was put down with extreme brutality. Intellectuals, lawyers and some military officers fled into exile.

Moi weathered this challenge with more repression, and went on to consolidate his power base by allowing corruption to take on ever more extravagant dimensions. Highly personalised executive power became his recipe for governing, and imperceptibly he had become an unchallengeable leader, barely recognisable as the invisible non-contestant of earlier years.

Daniel arap Moi is installed as president in 1978. Photograph: Jean-Claude Francolon/Gamma-Rapho via Getty

In the early 1990s persecution of ethnic groups associated with opposition or potential opposition led to hundreds of thousands of people being displaced in the Rift Valley, hundreds of deaths in so-called tribal clashes, and the detention of many political activists. Major scandals of misappropriation of government funds erupted, but were always hushed up. Such scandals – and ethnic cleansing – continued to the end of Moi’s regime. Ministers, politicians and senior civil servants also seized great tracts of public land, depriving thousands of poor agrarian people of their livelihoods.

In mid 1995 the first major political challenge came, with the founding of a new party, Safina (Noah’s Ark). It was led by Richard Leakey, the white Kenyan conservationist with a distinguished record of work for the country, and some well-known lawyers and old politicians disgusted with the decline in the country’s prestige and the isolation of Kanu from the increasingly impoverished population. The new party encountered violent opposition engineered by Kanu.

Reformist rallies for constitutional change held two years later were so violently attacked that 22 foreign embassies protested and the IMF threatened to hold back a $36m loan – the first of what became major sanctions by donors and the international financial institutions. By 2002, the IMF had withheld $350m.

Moi was too wily a politician to attempt to change the constitution to give himself another term in power, but in 2001 he began to prepare his departure and ensure his own future. He appointed to parliament Uhuru Kenyatta, the businessman son of the first president, quickly promoted him to minister for local government, and groomed him to lead Kanu. Moi, it was clear, would mentor the inexperienced young man.

Not only did this enrage the old party barons of Kanu, who were awaiting their moment of supreme power, but Kenya’s opposition parties managed to unite after a decade of squabbling. Together they produced an upset victory in the 2002 election that humiliated both Kenyatta and his promoter. Moi was forced to hand over power to Mwai Kibaki, formerly a key member of Kanu but for 10 years an implacable critic of all the Moi regime had come to represent.

However, by 2007, in the tough world of Kenyan politics, Kibaki found he needed Kenyatta – and made him deputy prime minister. Kenyatta was charged by the international criminal court with being one of those who perpetrated the deadly electoral violence that year. But this did not end his career and by 2013 politics in Kenya came full circle with the election of Uhuru Kenyatta as president.

By 2015 the ICC charges were dropped and Kenyatta was re-elected in 2017. With the election of Moi’s youngest son, Gideon, as the powerful chairman of Kanu as well as senator for Baringo from 2013, Moi in his old age saw his political legacy consolidated just as he had long planned.

In 1950 Moi married Lena Bommet, and they had five sons and three daughters. They divorced in 1979 and Lena died in 2004; his eldest son, Jonathan, died last year. Moi is survived by his other children.

• Daniel Toroitich arap Moi, politician, born 2 September 1924; died 4 February 2020