JAKARTA, Indonesia (AP) _ Parliament elected an ailing but revered Muslim moderate as Indonesia's new leader today, hours after President B.J. Habibie suffered a humiliating political setback and withdrew from the race.

Abdurrahman Wahid (pronounced Ahb-door-RAH-man Wah-heed), whose political shrewdness helped him outmaneuver the woman long held to be the front-runner for president, now faces the daunting task of restoring national unity and leading the world's fourth-biggest country out of its worst economic crisis in a generation.

It was the first free and contested election for head of state in Indonesia's 54 years of independence. There never has been more than one candidate in past presidential ballots.

In the historic vote, the 700-member People's Consultative Committee backed Wahid over pro-democracy opposition leader Megawati Sukarnoputri, the popular daughter of Indonesia's founding president, Sukarno, by a vote of 373 to 313, with five abstentions.

After Wahid defeated Megawati, both tried to overcome deep divisions in the country's many activist groups, some of which had threatened violent protests if the election didn't go their way.

``For the unity of the nation, I call on the people of Indonesia to accept the results of the election,'' Megawati said in parliament after she joined Wahid and hundreds of other legislators in singing Indonesia's national anthem following the vote.

``Together with Megawati I celebrate our independence and freedom,'' said Wahid, 59, better known by his nickname, Gus Dur.

After Megawati lost, explosions and clashes rocked Jakarta. Security forces fired tear gas and warning shots at a mob of at least 10,000 people who threw rocks and gasoline bombs after they were blocked from marching on Parliament. Toll booths were set afire, and cars and shops were vandalized.

Witnesses said at least four people, including two soldiers, were injured when a car exploded near the legislature. The cause of the blast was not immediately known, and there were unconfirmed reports of fatalities.

``The people wanted Mega. Now there will be a revolution,'' said one supporter, who identified himself by the single name of Ita.

Earlier, just before the lawmakers cast their ballots, five people were injured at the city's main traffic circle when a large parcel of fireworks exploded in a flower pot.

Some 30,000 Habibie supporters also protested on his home island of Sulawesi.

Financial markets rose sharply after Habibie dropped out of the race, then fell when Wahid was elected, with some analysts saying they were concerned about his poor health and lack of specific economic policies.

The JXS Composite stock Index, which jumped 10.1 percent before the vote count, fell by 2.4 percent after Wahid won, and closed 0.13 percent higher. Trading was stopped half an hour early because the market was overloaded with activity.

Megawati's party was the biggest winner in June 7 parliamentary elections, but it failed to gain a majority. Megawati, an inexperienced politician who was running for office for the first time, was unable to shepherd enough support in the assembly, composed of new parliamentarians and government and military appointees.

Conservative Muslims have opposed Megawati, arguing that a woman should not run the country, the world's largest Islamic nation.

Indonesia's powerful military said it would stand behind Wahid, who was to be sworn in as president late today. A vice president was expected to be chosen Thursday in another assembly election.

Some observers have questioned Wahid's suitability, pointing to the fact that he has suffered two strokes in recent years, and despite extensive eye surgery this year in Salt Lake City, Utah, remains almost blind.

Wahid, who has often called for religious and social tolerance in this diverse archipelago of 17,000 islands, said during the campaign that he would continue to introduce democracy and adopt economic reforms sought by the International Monetary Fund as part of its bailout program for the battered economy.

Habibie was appointed, not elected, in May 1998 when President Suharto stepped down after more than three decades of iron-fisted rule amid bloody riots and pro-democracy protests.

For decades, the assembly has been largely a rubber-stamp operation, playing its role in well-orchestrated elections and legislative actions but unable to show independence.

Democratic reforms instituted by Habibie paved the way for free parliamentary elections on June 7, but the moves were not enough to save the former Suharto protege's political career.

Megawati's Indonesian Democratic Party for Struggle was the big winner in June with 34 percent of the vote to 12 percent for Wahid's National Awakening Party. It placed third behind the Golkar Party, which enjoyed decades of dominance.

Habibie's hopes fell apart early today when the assembly rejected the accountability speech that he was required to give at the end of his term. He admitted he made mistakes in his 16 months in office but blamed most of the country's problems on his predecessors.