The fall of Moammar Gaddafi ends the rule of one of the most mercurial and menacing figures in recent history — the “mad dog” sponsor of international terrorism who allied himself with the George W. Bush administration’s war on terror; the pan-Arabist who at one time or another alienated nearly all of his Arab brethren; and the self-styled revolutionary philosopher who, in the end, was just another violent dictator clinging to power.

With his trademark sunglasses, flowing robes and jut-jawed insouciance, Col. Gaddafi — he bestowed the rank on himself after seizing power in 1969 — has long been one of the world’s more recognizable figures. For many Americans, he is also the reviled author of the 1988 bombing of Pan Am Flight 103 over Lockerbie, Scotland, which killed 270 people. And in the wake of the Sept. 11, 2001, attacks, fearing U.S. anger and needing international investment after years of sanctions, Gaddafi made himself over as a friend of the West, disavowing weapons of mass destruction and sharing intelligence on al-Qaeda.

Oliver Miles, a former British ambassador to Libya, called Gaddafi a “conspirator” who thought it important that “nobody could guess what he could do next.” Gaddafi exploited his unpredictability to keep his enemies off balance, and he reportedly survived numerous plots and assassination attempts to become one of the longest-serving rulers in the world until rebels drove him from power this week.

In “Libya’s Qaddafi: The Politics of Contradiction,” Libyan political scientist Mansour O. El-Kikhia wrote: “The rules of the game in Libya continually change,” and Gaddafi’s “genius . . . is his ability to maintain and manipulate this chaos . . . because the survival of his regime hinges on continued turbulence.”

Gaddafi never lost his reputation for eccentricity, traveling overseas with a swaggering, all-female security detail and pushing for such seemingly quixotic goals as the abolition of Switzerland. In his first visit to the United States, for the annual meeting of the U.N. General Assembly in 2009, he called in a rambling 90-minute speech for the unification of Israel and the Palestinian territories in a state he called “Isratine.”

The rapprochement with the West culminated that year when Gaddafi was invited as a guest to a summit of the Group of Eight leading industrial countries in Italy. He even shook hands with President Obama.

But there was little internal political reform to match the diplomatic offensive. Gaddafi continued his one-man rule atop a system that purported to delegate power to “people’s committees,” which he championed in his “Green Book.” He claimed to have relinquished power in 1977 and said Libya was “self-managed by the people.” In fact, his security forces quickly crushed any hint of dissent.

“I consider it a guide for all humanity,” Gaddafi said of his manifesto in a rare interview with Western reporters in 2004. “One day, the whole world will be a republic of masses, topple down all governments and parliaments.”

After 41 years, that day has arrived for Gaddafi, the “Brother Leader and Guide of the Revolution.”

Gaddafi was born in 1942 into a Bedouin family. As a young man, he was inspired by the anti-colonialism and socialism of Gamal Abdel Nasser in neighboring Egypt. At school, he formed friendships with a group of young men who would become his co-conspirators against Libya’s pro-Western monarchy. Gaddafi attended military college and spent several months getting further military training in Britain before being commissioned.

On Sept. 1, 1969, he and a group of young officers seized power in a bloodless revolution. The charismatic Gaddafi, only 27 at the time, soon emerged as the country’s paramount leader and quickly tried to establish himself as an anti-Western iconoclast. He forced out U.S. and British military forces and, over the next two decades, invited in every shade of radical from the Palestine Liberation Organization to the Irish Republican Army.

Gaddafi was an early enthusiast of an Arab political union and saw himself as Nasser’s natural successor. But nearly all his efforts to become an Arab liberator floundered, and Libya was often as isolated from its neighbors as it was from the West. Libya had small shooting wars with Egypt, Chad and Tunisia. Gaddafi clashed with PLO leader Yasser Arafat. He called for overthrowing the Saudi royal family.

Throughout the 1970s and 1980s, Gaddafi was infamous as a leading sponsor of international terrorism. In 1986, President Ronald Reagan, who called Gaddafi the “mad dog of the Middle East,” bombed Tripoli and Benghazi after Libya was linked to the bombing of a nightclub in Berlin that killed two U.S. servicemen and a Turkish woman.

Gaddafi said his 15-month-old adopted daughter was killed in the U.S. attack.

In 1987, a ship carrying 150 tons of arms from Libya and destined for the IRA was seized off the coast of France. It subsequently emerged that several other Libyan arms shipments had reached Ireland.

In the wake of the Lockerbie bombing, Libya was subject to U.S. and United Nations sanctions after Gaddafi refused to hand over two Libyans, including an intelligence officer, implicated in the terrorist attack. The intelligence officer, Abdel Basset Ali al-Megrahi, was eventually turned over and convicted. And in 2003, Libya accepted responsibility for the “actions of its officials” in a submission to the United Nations. Gaddafi continued to insist that although Libya accepted that one of its citizens was involved in the attack, it did “not mean the state is responsible for those actions.”

But by 2003, Gaddafi was on his way to a major reorientation of his relations with the West, particularly the United States. He was among the first Arab leaders to condemn the Sept. 11, 2001, attacks. Osama bin Laden was an old foe, and Libya had issued an international arrest warrant for the al-Qaeda leader in March 1998, several months before the group’s first major assault on the United States: the embassy bombings in East Africa.

Libya soon announced that it was abandoning a secret program to develop nuclear weapons, and it destroyed chemical munitions. The George W. Bush administration lifted sanctions, and foreign investment and international leaders, including Britain’s Tony Blair and France’s Nicolas Sarkozy, made their way to Libya.

“He’s perfected a persona, and part of that was to be strategically unpredictable,” said Jon B. Alterman, director of the Middle East program at the Center for Strategic and International Studies.

Gaddafi characterized the change as one of choice, not necessity.

“No one imposed isolation on Libya in the past,” he told the group of Western reporters. “We Libyans chose to isolate ourselves from the West in support of causes of liberation, like black South Africa and the Palestinians.”

The strategic change brought few benefits for ordinary Libyans except the ability to see the outside world more clearly and compare it with their own.

“One of the things that happened was that Libya was opened up to satellite television,” Alterman said. So the question, he said, became: “Why don’t we have the prosperity that other oil countries have?” Libya has about 46 billion barrels of oil reserves, the ninth-largest holding in the world.

Staff writer Robert Barnes in Washington and special correspondent Caryle Murphy contributed to this report.

Related stories on Libya

- The political rise and fall of Moammar Gaddafi

- Moammar Gaddafi through the years

- The battle for Libya

- Gaddafi bunkers and tunnels an elaborate setup

- Read more stories from around the world