Tanks and other armored vehicles rolled into this city's streets this morning as the Defense Ministry announced a nationwide alert because of what officials described as the threat of a U.S. attack.

Soldiers dug trenches for shelter from air raids and took up positions at scattered points along major roads. Militias received orders to stockpile food and to form teams to fight fires, provide first aid, and care for children and old people in case of attack.

In Brasilia, Secretary of State George P. Shultz called Nicaraguan fears "self-induced and based on nothing." But he added, during a press conference at the General Assembly of the Organization of American States, that the United States intends to "work in every way that we can to cast" Nicaragua's "aggression and subversive influence out of our hemisphere." Story, Page A20.

The alert, announced in "emergency communique No. 1" published in this morning's newspapers, was the government's most dramatic response to events since President Reagan's reelection that have been cited here as evidence of U.S. intention to take direct military action against Nicaragua. These included escalation of U.S. complaints about delivery of Soviet Bloc arms and five flights in four days by U.S. supersonic spy jets that caused sonic booms.

The alert thus reflected concern that Reagan, unencumbered by the political need to appear as peacemaker before the U.S. elections, would ratchet up pressure on the left-wing Sandinista government, diplomats and other political observers said.

But the mobilization also served the Nicaraguan goverment's domestic purposes following elections here eight days ago, these sources said. A crisis atmosphere helps to keep occupied the progovernment militants who previously were busy with the election campaign, and it provides a good excuse for a possible crackdown on the domestic opposition, they said.

Already some political liberties that were granted during the three-month Nicaraguan campaign were being withdrawn. The sole opposition newspaper, La Prensa, reported that the Interior Ministry had tightened censorship considerably beginning last Wednesday.

"I do think that many Sandinista leaders believe in an eventual U.S. military assault on this country. It is an article of faith with them," a foreign political observer said in discussing the mobilization. But he added that the alert "obviously does something for Sandinista cadres: It keeps their enthusiasm and sense of preparedness up . . . and it gives the government a marvelous excuse to move against the opposition and toward the militarization of the society."

Some opposition leaders complained that signs of growing U.S. pressure on Nicaragua, by encouraging hard-liners in the government here, was jeopardizing their own efforts to force the Sandinistas to assure political and economic liberties. These leaders, who have received U.S. backing, said the crisis atmosphere was giving the government a boost at a time when an opposition boycott of the elections here had embarrassed the Sandinistas abroad.

The U.S. campaign in recent days "hasn't done us any good," Dr. Luis Rivas Leiva, president of the Democratic Coordinator Alliance, said. The coordinator, an alliance of four political parties, two unions and the nation's largest business organization, is considered the principal opposition group.

"The Sandinista Front has shown that it handles itself much better in a military offensive than it does in defending itself against the civic opposition. We had them on the defensive," Rivas Leiva said.

In a separate development, most members of the coordinator withdrew from a "national dialogue" begun before the elections, reportedly in protest over the Sandinistas' refusal to postpone the ballot. Only the Social Christian Party and one of the coordinator's two labor unions remained in the talks, which were aimed at bringing the opposition back into the nation's political mainstream.

The alert appeared designed in large part to increase the number of activists involved in the militias and Sandinista mass organizations. Leticia Herrera, head of the Sandinista Defense Committees, said at a news conference that these neighborhood block organizations would increase their patrols and recruit more members.

"No citizen will stay home. No citizen will stay out of the civil defense. No citizen will stay out of militia training," Herrera said.

The alert marked the first time that tanks and armored personnel carriers have been stationed on Managua's streets. By midday, most were hidden on the sides of roads under piles of green branches placed on top as camouflage.

Militia members and other activists were reluctant to discuss their activities, but they were seen giving rifle instruction to some citizens and loading boxes of cartridges onto military trucks.

Stories quoting opposition leaders or other government critics, which frequently had appeared during the election campaign here, increasingly were being barred. Another of the censor's targets were wire service dispatches from Washington quoting U.S. government officials as expressing concern over Soviet arms shipments here or denying that the United States was contemplating an invasion.