David Cameron meets Nelson Mandela in 2008. In 1989, he visited South Africa on a trip funded by an anti-sanctions lobbying group. Photograph: Johnny Green/PA

Nelson's Mandela death has brought together politicians from across the political spectrum in shared tribute, obscuring a far less harmonious history in which attitudes towards the South African leader during the apartheid years divided along bitter ideological lines.

The shift is most apparent in the Conservative party. The union flag hung at half mast over Downing Street morning and David Cameron issued a lavish tribute, describing Mandela as "a towering figure in our time; a legend in life and now in death – a true global hero".

The praise was in stark contrast to the view taken by Cameron's predecessor and another of his heroes, Margaret Thatcher, who described the African National Congress as "a typical terrorist organisation" and fiercely opposed sanctions against the apartheid regime. Her South Africa policy was in part personal: her husband, Denis, had extensive business interests in the country. But her outrage at sanctions also sprang from her anti-communist convictions, which put the promotion of the free market above most, if not all, other political concerns.

As the apartheid apparatus began to crumble, Thatcher's instinctive support went to the Zulu leader Mangosuthu Buthelezi, who led the Inkatha Freedom party, the ANC's only serious rival, rather than to Mandela.

At the 1987 Commonwealth summit, her spokesman, Bernard Ingham, derisively rejected the suggestion that the ANC could achieve power. "It is cloud cuckoo land for anyone to believe that could be done," Ingham said, seven years before the watershed election that made Mandela president.

While Thatcher herself always claimed to oppose apartheid on principle and was eventually persuaded to take the ANC seriously as an opposition movement, some of her keenest supporters were far less restrained.

An arch-Thatcherite MP, Teddy Taylor, declared that Mandela "should be shot". One of her biggest fans in the press, the News of the World's "Voice of Reason", Woodrow Wyatt, accused Mandela and the ANC of trying to establish "a communist-style black dictatorship".

Sir Larry Lamb, a personal friend of Thatcher and then Daily Express editor, declared in 1985 that Mandela's unconditional release would be "a crass error".

During Thatcher's time in office, members of the Federation of Conservative Students (FCS) went as far as wearing stickers declaring: "Hang Nelson Mandela" until the group was banned in 1986 by an embarrassed Tory leadership. The head of the FCS at the time, John Bercow, is now the Speaker of the Commons, but he has insisted he did not take part in the Mandela-baiting.

Nor is there any evidence that the young David Cameron was involved. However, he did visit apartheid South Africa in 1989, when he was 23, on an all-expenses-paid "fact-finding mission" funded by Strategy Network International, a lobbying group seeking to lift sanctions.

The demonisation of the then political prisoner had its roots in racial attitudes but also in the black-and-white cold-war world of ideological struggle, in which Mandela's ties with the South African Communist party mattered far more than their common opposition to a deeply unjust system.

Cameron broke with previous Conservative policy on South Africa while he was opposition leader. Returning in 2006 from South Africa, where he met Mandela for the first time, he wrote in the Observer: "The mistakes my party made in the past with respect to relations with the ANC and sanctions on South Africa make it all the more important to listen now."

The attempt to bury Tory ghosts drew accusations of betrayal from Thatcher aides such as Ingham, who remarked: "I wonder whether David Cameron is a Conservative."

The party chairman during the Thatcher era, Lord Tebbit, was also unrepentant. Challenged over the Tories' demonisation of Mandela as a terrorist in the 1980s, Tebbit told BBC Radio 4's The World at One: "He was the leader of a political movement which had begun to resort to terrorism. You have to act within the constraints of the time and I get very irritated by people who judge the past by the present."

However, such sentiments have been rare across the political landscape in the reaction to Mandela's death. Perhaps uniquely among the world's political leaders, he has long been an icon of freedom, justice and tolerance with whom all politicians strive to identify.