JOHANNESBURG, South Africa - One of the ironies of the praise from world leaders for Nelson Mandela at a state memorial service Tuesday is that the former South African president frequently irritated the United States and others with his positions and loyalty to longtime supporters such as Cuba and Libya.

Both Cuban leader Fidel Castro and Libya's Moammar Gadhafi had long supported the African National Congress. And as president, Mandela did not abandon them, despite their human-rights abuses and despite Libya's involvement in the 1988 Lockerbie bombing in which a Pan Am transatlantic flight was destroyed over Scotland, killing 270 people.

Mandela endorsed human rights as "the core of international relations" in an article in Foreign Affairs magazine just before taking office in 1994. But he also said in 1998: "My foreign policy is determined by the past. . . . The relations I have had with the country, the contributions they have made to our struggle."

"We will never renounce our friends," Mandela said in February 1996 during a visit to Robben Island with Norwegian Prime Minister Gro Harlem Brundtland. He said he would invite both Castro and Gadhafi to South Africa despite pressure from the United States to abandon his support for them.

Castro did visit South Africa during a conference of nonaligned nations in 1998, but Gadhafi never came. Mandela visited Libya several times after his release from prison in 1990, partly to raise funds for the ANC, according to a definitive biography by the late British journalist Anthony Sampson.

"Mandela was profoundly loyal to those who supported the liberation struggle, even if it was in their narrow self-interest to do so and when it had little or nothing to do with nonracial democracy," said John Campbell, senior fellow for Africa policy studies at the Council on Foreign Relations.

Even the administration of President Bill Clinton, who attended the memorial service Tuesday, did not escape Mandela's finger-wagging. In October 1997, as Mandela was preparing to visit Libya, the State Department said it would be "disappointed" if he went ahead. Mandela replied at a banquet in Johannesburg: "How can they have the arrogance to dictate to us who our friends should be?"

When he visited and embraced Gadhafi, he criticized "countries that play policeman of the world" and said "those who object to my visiting Libya have no morals."

Mandela and Clinton also differed over whether to give another term as U.N. secretary-general to Egyptian politician Boutros Boutros-Ghali. Though Boutros-Ghali had widespread support, the Clinton administration blocked his reappointment. Mandela was, however, partly mollified by the selection of Kofi Annan from Ghana.

Mandela remained a staunch supporter of Clinton despite the friction over diplomacy.