Sadiq Mahdi, who has emerged from elections here as the likely leader of a new civilian government in Sudan, once invaded his own country with the help of Libya's Col. Muammar Qaddafi.

Yet Mahdi, a friend of Qaddafi for 15 years and a frequent visitor to Tripoli, also is the one Sudanese political figure who the Reagan administration has been hoping would become this country's prime minister.

For a pious Moslem politician in a strategically positioned nation to garner support from the world's two most publicized antagonists is a neat trick. But for Mahdi, whose Umma Party won a plurality of 48 percent of the vote in Sudan's first multiparty election in 18 years, a more formidable test of political agility is ahead. To succeed as the leader of Africa's largest and one of its most troubled countries, students of Sudanese politics agree, he must act adroitly to reconcile sworn enemies fighting a growing civil war in his country.

At the same time, economists warn that Sudan's likely new leader must take politically painful steps to rebuild an economy shattered by drought and mismanagement.

"Sudan is one of the world's basket cases," said an international economist here. "It is an act of courage to want to be its leader."

The election, which took place from April 1-12, proved to be both peaceful and honest. At stake were 264 seats for a 301-member assembly, with voting in 37 southern districts postponed because of the civil war. The assembly will meet in coming weeks to write a constitution and pick a government.

As the likely leader of a coalition government expected to take power at the end of this month, Mahdi soon will be forced to play his hand.

Mahdi, 51, is by birthright a leader in this country that is three times the size of Texas. He is an Oxford-educated descendant of Sudan's most celebrated religious-military leader. In the 1880s, his great-grandfather, the Mahdi, liberated Sudan from Turkish rule and routed the British from Khartoum.

As the hereditary leader of the Ansar, the influential religious sect founded by his great-grandfather, Mahdi has held power or been struggling to regain it nearly all his life. To that end, according to a western diplomat who has watched him for years, Mahdi has shown himself to be a "very practical man."

That pragmatism has allowed Mahdi to declare himself a leader of "awakening Islam" in Sudan while at the same time condemning and campaigning against Islamic extremists.

Ten years ago, Mahdi sought asylum in Qaddafi's Libya and from there staged an abortive invasion of Sudan. But western diplomats here say Mahdi made only a marriage of convenience in Libya. They believe Mahdi is prowestern and wary of Qaddafi.

"I'm not sure who the real Sadiq Mahdi is," said a diplomat here, echoing the uncertainty voiced by many Sudanese and foreign officials who have observed Mahdi's career and read his opaque political writings.

"The man has had to waffle for the sake of the election."

As it has been for most of the past quarter century, the overriding crisis facing the Sudanese government is civil war. That war pits the Moslem north against Christians and animists in the south.

The north considers itself part of the Arab world. The south considers itself part of Africa. In Khartoum, for example, the U.S. bombing last week of Libya provoked outrage among northerners, but southerners were indifferent.

The north, which traditionally has controlled the government here in the capital, wants unity and some revenue from the resource-rich south. The south fears and resents Moslem domination.

In this country of 21 million people, one-third of whom live in the rebel-controlled south, the election attempted to address north-south animosity by giving every citizen an equal voice in choosing a national government.

The election was supposed to purge Sudan of bitter memories of president Jaafar Nimeri, who was deposed a year ago after 16 years of erratic and dictatorial rule.

But southern rebel leader John Garang ordered his supporters not to participate in what he called "partial elections." In an apparent attempt to increase his negotiating leverage with the winner of the election, Garang picked up the pace of rebel fighting in the south.

He forced cancellation of voting in more than half the southern constituencies, and his rebels have been implicated in the killings of two southern candidates for the national assembly.

Garang's Sudanese People's Liberation Army is considered by western military analysts to be too well-armed to be defeated by Sudan's Army.

Many Sudanese in Khartoum say the only solution to civil war, which the Sudan government cannot afford to fight, is negotiations that would guarantee a measure of political autonomy to the south.

In addition, the rebels have made two nonnegotiable demands that grow out of the south's ancient fear of Moslem domination. One demand is that Khartoum abrogate the Islamic law that was instituted under Nimeri and has produced nearly 200 court-ordered amputations, most of them for petty theft. Second, the south insists on a secular rather than an Islamic constitution.

Mahdi has been sympathetic to both these demands. If he had won an absolute majority in the election, his advisers say, he would have gutted Islamic laws and assured the south its religious autonomy.

However, the fundamentalist National Islamic Front, previously known as the Moslem Brothers, made an unexpectedly strong showing in the election. Including absentee ballots, many political analysts expect the Islamic front to control nearly one-fourth of the seats in the constituent assembly.

The leader of the fundamentalists, Hassan Turabi, vowed in an interview last week that his party would fight to keep Islamic law, which Turabi helped institute and wants to strengthen. Amputations, he said, have "a dramatic effect on the street."

Turabi accuses Mahdi of pursuing a brand of Islam "without character, without teeth."

"Our group is going to be quality-wise very powerful. It will be very difficult for Sadiq to pass" any change in Islamic law, said Turabi, who has a doctorate in law from the University of Paris. Mahdi's advisers have conceded since the election that it will be difficult to get rid of Islamic law.

Yet, without an end to the harsh legal code, most analysts here predict continued and escalating civil war that increasingly will involve outside powers.

The war already has drawn in Ethiopia, which is both patron and sanctuary for the southern rebels. Some western diplomats believe the rebels have lost control of their movement to Soviet-influenced Ethiopia.

In the past month, Libya, which used to support the rebels, has begun shipping arms, equipment and bomber aircraft to the Sudanese government. U.S. officials fear that the quid pro quo for Qaddafi's guns will be Sudanese-Libyan economic unity or Sudanese participation in Libya's adventurism in Chad or even Egypt.

It is still too early to say if Turabi can make good on his vow to prevent any change in Islamic law. To maneuver around the fundamentalists, Mahdi needs support from the Democratic Unionists Party, which won 31 percent of the seats in the new assembly.

That support was placed in doubt last week when the party's secretary general, Omer Nour Daim, insisted in an interview with a local newspaper on a role in the new government for Turabi's fundamentalists.

Without an agreement with southern rebels, economists here say a new government cannot begin to rebuild an economy that has been moving backward in the last five years. Chronic drought, runaway inflation, bloated bureaucracy, nearly total dependence on unstable cotton prices and unpayable foreign debts all add up to what economists say is one of the most fragile and inefficient national economies in the world.

These economists agree, however, that it makes no sense to repair the national economy if civil war continues to cut Sudan in half. They say Mahdi is right to subordinate economic issues to ending the war.

It remains to be seen if Mahdi will be able to build a government capable of accommodating Islamic extremists who insist on laws that well-armed rebels in the south consider unacceptable. Sudanese political observers say this is a daunting, if not impossible, order, even for a master of moderation who can expect economic support from sources as inimical as Reagan and Qaddafi.