Africa has 54 countries and more than a billion people. One of the most ridiculous myths about it is that homosexuality did not exist in the continent until white men imported it. Robert Mugabe is one such propagator, calling homosexuality "un-African" and a "white disease".

Throughout history people everywhere have explored and experimented with their sexuality. The desire to do so has never been confined to particular geographical locations. Its reach is universal. Yet today the myth of a pre-colonial sexual innocence, or more fittingly, ignorance, is used to endorse anti-gay legislation and stir up homophobia and persecution in Africa. In my father's country, Nigeria, a new law passed in January carries a 14-year prison sentence for same-sex marriage and up to 10 years for membership or promotion of gay groups. In Uganda, the Anti-Homosexuality Act can impose life imprisonment. Latter-day evangelicals from the US are partly to blame for this continuing persecution, but so are Africa's political leaders such as presidents Yahya Jammeh of the Gambia and Yoweri Museveni of Uganda, who use rabble-rousing anti-gay rhetoric to increase their power base and popularity.

While much has been written about this dangerous turn of events, little has been written about its origins. Two trailblazing studies in the field – Boy Wives and Female Husbands edited by Stephen O Murray and Will Roscoe, and Heterosexual Africa? by Marc Epprecht – demolish the revisionist arguments about Africa's sexual history. From the 16th century onwards, homosexuality has been recorded in Africa by European missionaries, adventurers and officials who used it to reinforce ideas of African societies in need of Christian cleansing.

The Portuguese were among the first Europeans to explore the continent. They noted the range of gender relations in African societies and referred to the "unnatural damnation" of male-to-male sex in Congo. Andrew Battell, an English traveller in the 1590s, wrote this of the Imbangala of Angola: "They are beastly in their living, for they have men in women's apparel, whom they keep among their wives."

Transvestism occurred in many different places, including Madagascar and Ethiopia. Among the Pangwe people of present-day Cameroon and Gabon, homosexual intercourse was practised between males of all ages. It was believed to be a way to transmit wealth. The Nzima of Ghana had a tradition of adult men marrying each other, usually with an age difference of about 10 years. Similar to the pederasty of ancient Greece, Sudan's Zande tribe had a tradition of warriors marrying boys and paying a bride price, as they would for girl brides, to their parents. When the boy grew up, he too became a warrior and took a boy-wife.

In this same tribe lesbianism was practised in polygamous households. In the 18th century the Khoikhoi of South Africa used the word koetsire to describe men considered sexually receptive to other men, and soregus was the word they used for a friendship which involved same-sex masturbation.

Homosexuality is also recorded among the Siwa of Egypt. It was considered a boy's rite of passage in Benin, and woman-woman marriages involving a bride price existed in more than 30 African societies from Nigeria to Kenya to South Africa.

How far back can homosexuality be traced in Africa? You cannot argue with rock paintings. Thousands of years ago, the San people of Zimbabwe depicted anal sex between men. The truth is that, like everywhere else, African people have expressed a wide range of sexualities. Far from bringing homosexuality with them, Christian and Islamic forces fought to eradicate it. By challenging the continent's indigenous social and religious systems, they helped demonise and persecute homosexuality in Africa, paving the way for the taboos that prevail today.

The main character in my latest novel, Mr Loverman, is a 74-year-old black gay man, Barrington Walker. Married with two daughters, he has been in the closet for 50 years. Soon after the book was published, a young gay man emailed me from Nigeria expressing his fear that his life would turn out like Barrington's. I didn't know what to suggest except that, if he wanted to live openly and legally as homosexual, he had to leave his homeland. What else could I say?

Millions of gay people living in Africa face a similar choice. If they stay, they can either repress their natural sexuality or risk losing their liberty and their lives. The legacy of colonialism is alive and well. As another character in Mr Loverman says: "It's homophobia, not homosexuality, that was imported to Africa."