The Nigerian military government of Maj. Gen. Mohammed Buhari, which struggled for 20 months to cure the country's massive economic problems, was overthrown today in an apparently bloodless coup led by the regime's third-ranking military leader.

Maj. Gen. Ibrahim Babangida, 44, a career military officer who has played a pivotal role in three previous Nigerian coups, was named this afternoon on Nigerian television as the country's new president and military commander.

The country's air and sea ports were sealed off, communications links with the outside world severed and a dawn-to-dusk curfew announced in major urban centers. Nigeria's land borders have been closed since shortly after the ousted government took power.

In an address tonight on Nigerian television, Bagangida said the Buhari government had been "too rigid and uncompromising," had failed to end "economic mismanagement" and had caused "intolerable suffering." He also promised to release several Nigerian journalists who had been jailed under a 16-month-old law restricting criticism of the government.

Earlier, in announcing the takeover in Africa's most populous nation, the sixth in the 25 years since Nigeria's independence from Britain, an Army officer said the action was taken because the Buhari government had misused power and wasted the country's resources. "We could not stay passive and watch a small group of individuals misuse power to the detriment of our national aspirations," said Brig. Joshua Dogoniaro on Lagos Radio this morning.

Nigeria size and oil wealth, as well as its estimated 100 million population, underpin its influence in Africa. It also is an important trade partner of the United States, which imports about half of the country's oil output.

Buhari came to power at the end of 1983 in a military takeover and pledged to resuscitate the Nigerian economy by ending the corruption and economic mismanagement of the 4 1/2-year-old civilian government of Shehu Shagari.

The coup that brought Buhari to power was "welcomed by the nation with unprecedented enthusiasm," the Army spokesman said this morning. But, "almost two years later," he added, "it has become clear that the fulfillment of expectations is not forthcoming." The goals of the 1983 takeover "have been betrayed and discarded."

Buhari's whereabouts were unclear today. Western diplomats in London said that Buhari had been in Lagos, the Nigerian capital, yesterday. Lagos was reported calm today after the coup.

In Washington, the State Department said there was no indication of any dangers to the 7,000-strong American community in Nigeria. "We have had a good relationship with Nigeria based on a convergence of enduring national interests . . . . We expect that this relationship will continue," spokesman Charles E. Redman said.

In Santa Barbara, Calif., White House spokesman Larry Speakes said that "Lagos is calm and radio announcements are encouraging peace and calm," The Associated press reported.

The toppling of the Shagari government in 1983 brought an end to an experiment in multiparty democratic rule that was regarded as a political milestone in black Africa.

Buhari, the new leader, moved quickly to implement his pledges to revive the economy by imposing austerity measures that laid off government workers, slashed social benefits, canceled industrial projects and kept Nigerians from spending their money on imported luxuries.

A succession of harsh laws were passed to stop corruption, smuggling, street violence and curb political dissent. The government mounted a highly publicized "war against indiscipline."

Buhari's tough policies, however, while helping to trim the deficit and making Nigeria slightly more livable, were not enough to turn around the economy.

Nigeria, which boomed in the 1970s, becoming almost completely dependent on its large oil reserves, was not able to recover from a sharp drop in price and demand for oil. Its oil earnings, accounting for about 90 percent of its revenues, are about half what they were in 1980.

Inflation is raging at more than 40 percent a year, compared to half that figure 18 months ago, unemployment has surged, there are food shortages and nearly half of the country's export earnings are consumed in servicing a $21 billion foreign debt.

Under Buhari, Nigeria had refused to negotiate an agreement with the International Monetary Fund. The agreement, which required Nigeria to sharply devalue its currency, open its markets to imports and cut domestic fuel subsidies, would have bailed the country out of its debt squeeze by allowing it to reschedule loans.

But in press interviews this year, Buhari said the IMF's medicine was politically impossible and that Nigeria must solve its own problems by tightenting its belt. He declared that Nigeria is "no longer begging anybody" and warned of "very tough" years ahead for Nigerians.

Today's coup, according to diplomatic observers here, indicates that the military officers who orchestrated the 1983 coup and turned power over to Buhari, an experienced administrator, were unwilling to accept his stringent long-term economic prescriptions.

One of the leaders of the 1983 New Year's Eve coup was Babangida, the new military leader and Army Chief of Staff on the Supreme Military Council.

Although he has no experience as a government administrator, he has a reputation as a a bold and decisive military leader. He made his reputation among lower and middle ranks of the armed forces in 1976 when he walked unarmed into the offices of a radio station and persuaded Army officers attempting a coup to surrender peacefully.

According to western diplomats here, a year before Babangida helped foil the 1976 coup attempt, he helped lead a coup that overturned the 9-year-old government of Gen. Yakubu Gowon.

Babangida, who received military training in Nigeria, India, Britain and the United States, also is credited with having been a leading organizer of the 1983 coup that toppled Shagari. Diplomats say the general was disappointed when he was passed over in that coup.

Babangida also was outranked by Maj. Gen. Tunde Idiagbon, chief of staff of the Supreme Military Council who was responsible for much of the day-to-day management of the government.

Babangida is a Moslem from northern Nigeria, one of a succession of northern Moslems, both soldiers and civilians, who have led the country since 1975.

In recent months, there have been increasing report of dissatisfaction among Nigeria's southern, mostly Christian population over the dominance of northern Moslems in the government.

Support for Buhari reportedly waned sharply in the last year with the imposition of measures restricting freedom of speech and the press. In April 1984, the military government issued a decree that imposed harsh penalties for "disseminating false information about the government." The law muzzled Nigeria's traditionally free press.