With a final show of determination to defend its neutrality, Sweden today released the Soviet submarine it had accused of deliberately violating Swedish territorial sovereignty while carrying nuclear weapons.

Soviet Submarine 137, which ran aground Oct. 27 near sensitive Swedish military installations in the rocky Karlskrona archipelago, was towed to sea by Swedish tugboats this morning. The sub was then allowed to continue on its own and join a flotilla of 11 Soviet warships and salvage vessels waiting outside the 12-mile limit of Sweden's territorial waters.

High winds and rough seas, officials said, prevented the Swedes from towing the submarine as far as had been planned for "political reasons." But the Swedish Navy shadowed the submarine with a helicopter and a number of patrol and torpedo boats, plus other craft carrying journalists.

A Swedish pilot went just ahead in another boat to guide the gray submarine flying the Soviet flag and ensure that it did not submerge before leaving Swedish waters. A Swedish icebreaker moved from where it had been blocking the narrow channel out of the archipelago, and Swedish soldiers guarded island coastal batteries protecting the nearby Karlskrona naval base.

This calculated embarrassment to the Soviets was added to by Sweden's Scandinavian neighbors, NATO members Denmark and Norway, who announced today they were joining Sweden in ordering their ambassadors to boycott Saturday's annual military parade in Moscow marking the anniversary of the Soviet revolution.

Danish Foreign Affairs Minister Kjeld Olesen also said at a press conference in Copenhagen that the Nordic nations' confidence in the Soviet Union had been undermined by both the submarine affair and Denmark's recent expulsion of a Soviet diplomat accused of espionage. The second secretary of the Soviet Embassy in Copenhagen, Vladimir Merkoulov, was expelled last week, according to Danish sources, for helping stir up anti-NATO sentiment by participating in advertising campaigns and meetings of leftist political groups.

"The two affairs have strained relations between the Nordic countries and the Soviet Union," Olesen said. "It will be a long time before the trust is restored."

Swedish Foreign Minister Ola Ullsten said here yesterday that "this incident will have an effect on Swedish relations with the Soviet Union for some time."

A senior diplomatic source added, however, that Sweden would try to avoid "a long-term poisoning of relations" that could be counterproductive.

"What has happened has already been a blow to relations," the source said. "Our protests have been extremely strong and determined."

The Swedish government saw the affair, diplomats said, as an important test of Sweden's central foreign policy of "being nonaligned in peace-time aiming to be neutral in war."

"I hope this has shown what Swedish neutrality means and that we are determined to defend our sovereignty," Ullsten said yesterday. "Swedish neutrality must be respected abroad, and we must have the right to prevent these incursions into our territory."

The Swedish government was under considerable public pressure to take tougher legal and possibly military action against the submarine and its captain and crew, according to officials. But Ullsten said it decided to act in accordance with international law and set what could be a precedent for handling such incidents.

Ullsten had earlier refused demands by a number of Swedes that the submarine's captain and crew be exchanged for Raoul Wallenberg, the Swedish diplomat presumed captured by the Soviets in 1945 and who some believe is still alive in a Soviet prison.

Part of Sweden's neutrality policy consists of speaking out frequently against what it sees as violations of international law and human rights. It has been a leading European critic of both U.S. conduct in the Vietnam War and the Soviet Union's invasion of Afghanistan.

But some Swedish officials acknowledge that the submarine's incursion into a restricted military area well within Sweden's territorial waters raised questions about the strength of the military defense Sweden needs to maintain the credibility of its neutrality. They said it must be made clear that Sweden would and could repel any attack or attempt at coercion by any nation.

This also is considered vital to the so-called "Nordic balance" among Norway, Denmark, Sweden and Finland, which also considers itself neutral but has military and trade ties with the Soviet Union. Expressing its own very cautious disapproval in Helsinki today, the Finnish Foreign Ministry said, "The deplorable submarine incident is primarily a bilateral affair between Sweden and the Soviet Union."

Swedish officials said they believed the submarine affair would make Swedes more receptive to spending increases sought by the military to improve air and coastal defenses. Sweden spends as much of its gross national product on defense as many NATO nations. It depends on a universal draft, rapid mobilization of reserves, fast ships for patrolling its coastal waters, high-performance fighter aircraft, advanced-technology weaponry and ingenious use of terrain, including protective shelter for planes and other equipment in tunnels and caves blasted out of the landscape.

But Swedish defense spending has failed in recent years to keep up with the rising cost of new technology, and its military forces have been steadily shrinking in active personnel and equipment. Military spokesmen frequently pointed this out when discussing the incursion of the Soviet submarine.

Sweden makes most of its own military equipment to reduce dependence on other powers, but it still requires a continuing flow of Western technology for key components. Swedish officials said the government's primary goal during the recent visit of U.S. Defense Secretary Caspar W. Weinberger was to explain Sweden's policy of armed neutrality and its role as a buffer between East and West so that the United States will continue to help provide needed military technology.

"We wanted to convince him that this technology transfer to Sweden was okay," said a Swedish official, who did not want to be identified. "It had nothing to do with feeling threatened or seeking to lean toward the West more than before. It is still the same policy of Swedish neutrality."