Rebel guerrillas and National Guard troops engaged in heavy fighting in Nicaragua's capital city for the first time today, but President Anastasio Somoza's forces appeared to be reestablishing control in other key cities.

The civil war, in which the Sandinista guerrillas are attempting to topple Somoza's authoritarian rule, came to the capital in full force as steady fire from tank cannon, artillery shells and automatic weapons rang throughout the city of 500,000 people.

The shooting, far heavier and more persistent than earlier outbreaks, began around midday in neighborhoods ringing the center of the city. It came within blocks of the main National Guard garrison that houses the office and residence of Somoza and continued into the night.

[In Washington, the State Department announced that dependents of the embassy staff in Managua are being pulled out, with 60 having left already by commercial airline and another 30 to depart Monday.]

While the guerrillas appear finally to have brought their attacks to Managua, they have been forced over the past two days to give up nearly all ground previously won in the south near the Costa Rican border.

It was in the south, two weeks ago, in an invasion across the border, that the Sandinistas launched what they said was the final offensive in their long war against Somoza.

Yesterday, the National Guard declared the southern front won. Journalists flown by the government to the border area said it appeared quiet and under Guard control.

Although the government claimed 136 guerrilla dead in the two weeks of fighting, the journalists were shown only six bodies lined up in front of a small shack in El Naranjo, a plantation near the frontier.

Masaya, a city of 50,000 population 20 miles south of here where the guerrillas began fighting last Wednesday, lay in ruins today. Rebel bodies were still smoking in the streets where National Guard troops had set them on fire to prevent disease in the tropical climate.

Nearly every building in the city, which suffered severe damage in the initial outbreak of civil war last September, was pocked.

Many buildings were destroyed from air-launched rockets and heavy-caliber weapons. Blocks around the local military garrison, which Sandinistas had surrounded until National Guard reinforcements drove them out last night, were little more than charred rubble.

Heavy fighting continued in northern cities. The guerrillas still appeared in control of Matagalpa, 80 miles from here, although they spent the day under heavy aerial bombardment. Reporters attempting to enter the mountain city were pinned down by crossfire in the hills less than a mile outside and forced to turn back.

The loss of the southern zone is a heavy blow for the Sandinistas, who barely two days ago seemed to have been gaining the upper hand in widespread areas. With the National Guard now ostensibly able to divert troops and equipment from the border area, the recapture of Masaya last night and today's northern bombardment seem to indicate that the situation may become a repeat of last September.

At that time, the Guard allowed the Sandinistas to move into the cities and set up barricades. Rather than fight them in the streets, however, the National Guard tactics were primarily to surround the guerrillas on the city perimeters and use air-launched rockets and artillery to rout them.

Despite government claims that the guerrillas have been heavily armed by outside governments in the past several months, they have seemed little better a match for the Guard's fire-power in a lengthy battle this time than in September.

Except for an occasionally lucky shot from one of the Sandinistas' apparently limited number of .50 caliber machine guns, Somoza's aging air force of small Cessnas, C47 transports and T33 jet trainers operates with impunity.

While the guerrillas incessantly dig deep trenches across main highways to inhibit National Guard convoy movements, the Guard just as quickly fills them up again.

A convoy of Guard reinforcements was successfully ambushed outside Matagalpa last week, but yesterday the government shuttled troops to the hills around the city by helicopter.

Whether for lack of explosives, logistics or intent, the Sandinistas have not even attempted to blow up any of the many small bridges along highways leading to the main cities.

At the same time, government claims of guerrilla casualties seem wildly exaggerated. In Masaya today, Gen. Fermin Meneses said 80 Sandinistas had been killed and he put government losses at only at eight dead and six wounded - despite the four-day siege of the garrison.

Troops inside the garrison today said they had taken no prisoners. "We don't take prisoners. "We don't take prisoners," one officer said, "They have all died."

Judging from the numbers of bodies counted by those entering cities after Guard "cleanup operations," it appears that most of the Sandinistas have once again run into the hills to regroup, as they did in September.

The difference this time around is that instead of then traveling back to camps in the Nicaraguan countryside and in northern Costa Rica, many of the guerrillas have apparently come to Managua.

Today, for the first time since the sporadic civil war began last year, the few people venturing outside their homes in Managua carried white flags. It was a depressingly familiar sight to those who have watched refugees straggle from other cities under attack both in this round and in September.

Managua streets were almost entirely deserted today except for military vehicles. Within the city, the government advanced an 8 p.m. curfew imposed last week to 7 p.m.

Widespread fighting began in many of Managua's poor and middle-class neighborhoods two nights ago. It had continued into the morning and ceased by noon yesterday.

Today, the guerrillas and civilian supporters began constructing barricades in the barrios early in the morning. At noon, Guard contingents set out from the main barracks to tear them down.

For several hours, foreign reporters here were prohibited by government troops from leaving their hotel, located on a hill in the center of the several square miles that make up Managua.

From the hotel roof, the shooting could be heard and often seen in all directions - loud cannon booms, bombs, sustained automatic weapons fire, and the ping of handguns and small-caliber rifles.

At around 6:30 p.m. the sound of firing was overpowered as Somoza's personal helicopter took off from the central garrison, rose quickly over the city and headed off across Lake Managua on its northeastern side. There was no indication where the president, if he was on board, was headed. CAPTION: Picture, Nicaraguan National Guard troops enter streets of Masaya in counterattack. AP