Eighteen hours after it started with the hostage-taking of every major elected political figure in Spain, a coup attempt ended today with the surrender of the chief plotter and the release unharmed of all his captives.

Shortly after noon, coup leader Antonio Tejero Molina, 49, a lieutenant colonel in the paramilitary Civil Guard, was taken to the Guard's national headquarters to surrender to his own commander-in-chief. His expressed wish to surrender at the El Pardo Palace, Generalissimo Francisco Franco's old residence, was denied.

Tejero's dramatic gesture -- he burst in shooting a pistol and kidnaped the lower house of parliament -- fell apart when the support he apparently was banking on from the Spanish armed forces failed to materialize.

"You can all leave," he said to the 350-member parliament when the negotiations were over. "Nothing has happened. All I know is that this will cost me 30 to 40 years in jail."

Parliamentary speaker Landelino Lavicla, who was presiding over the chamber when Tejero burst in at the head of the first force of 20 rebel Civil Guardsmen at about 6:30 p.m. yesterday and forced him off the dais at pistol point, immediately returned to the tribune to announce that business as usual would be resumed on Wednesday. The main point on the agenda will be the vote that Tejero interrupted for the investiture of Leopoldo Calvo-Sotelo to succeed Adolfo Suarez as prime minister at the head of a center-right Cabinet.

In often emotional statements after their release, the deputies nevertheless seemed to share a consensus that things could never be as before among them. Several expressed a desire to work more closely together despite political differences to overcome the humiliation heaped upon the country's political leadership. It was not clear, however, whether these sentiments of the moment could be translated into a broadening of the moderate majority that has steered Spain through a five-year transition from dictatorship to democracy.

The feeling of having faced a common enemy was all the more intense as some of the hostages believed during the night that dictatorship had returned to Spain.

"I was convinced that we would be moved from parliament to a concentration camp," a socialist member of parliament admitted after his release.

The strain of the kidnaping showed as the rumpled hostages emerged free from the parliament building today. Rafael Calvo Ortega, the normally taciturn secretary general of the ruling centrist party, burst into tears. A common theme in their first statements was of the fear they had for their own lives and for the future of representative government here.

"Now is the time to reorganize the state," said Socialist Deputy Luis Solana. "We are going to have to restructure Spain. We all risked our lives, from Fraga to Carrillo."

He was referring to former Franco minister Manuel Fraga Iribarne at one end of the spectrum and to Communist Party leader Santiago Carrillo at the other.

Despite striking accounts of individual acts of courage by some leading deputies, the spectacle of the parliament face-down on its belly in response to the threats of a fanatical colonel seems likely to tarnish the still-fragile image of Spanish democracy. The only institution to have emerged from the testing with its prestige enhanced was the constitutional monarchy headed by King Juan Carlos, who worked through the night to prevent the armed forces from following Tejero's lead.

In addition to a solemn three-minute televised appeals for respect of the constitutional order, the king personally called each of Spain's nine major regional military commanders to head off any temptation to rally to the coup. The calls even included one to Capt. Gen. Jaime Milans del Bosch of the Valencia region, who had called out his forces in apparent support of the coup.

Milans del Bosch was arrested tonight and relieved of his command, despite his decision after Juan Carlos' speech to rescind a state of emergency in Valencia and order his troops back to the barracks.

That decision seems to have been the key factor that convinced Tejero not long before dawn that the time had come to start negotiating his surrender. nAs one of the hostages said, Tejero appeared to have become "the prisoner of his prisoners."

But not before two moments when, as one deputy put it, "I thought we were done for."

The first came at the start of the drama. Gen. Manuel Gutierrez Mellado, who holds the Cabinet post of deputy premier with overall responsibility for defense, started forward to confront the rebel leader. Tejero "acted as every subordinate does to a superior officer when he is staging a coup -- he drew his gun," saidthe deputy.

That was when a volley of shots rang out. The coup leader kept pushing Gutierrez Mellado until he finally returned to his seat on the government front bench. Then the rest of the Cabinet, following his example, sat up in their seats while most of the other parliament members stayed out of sight under their desks. The scene was rebroadcast by Spanish state television after the deputies had been released.

Unbeknownst to the occupiers of the parliament, there was a remote control camera that filmed the whole 18 hours.

About three hours before the denouement, the second crisis point occurred. Fraga Iribarne, the leader of parliament's conservative minority party, stood up from his seat and said that he had had enough.

"This is an assault on democracy," boomed the irate Fraga. "I am leaving."

The accumulated night-long tension broke with the exhausted deputies suddenly shouting, "Long live Spain. Long live democracy."

Tejero rushed into the hall and again pulled out his pistol; several of the 200 or so rebel Civil Guardsmen armed their automatic weapons. Silence ensued with only Fraga scuffling and shouting as he was led out of the chamber to be placed in a separate place of detention.

Analysts believe that another important factor in the coup's undoing was the unusual dispersal of the Brunete Armored Division, the most powerful coup-minded force in the Madrid region. The division was involved in a coup scare a year ago, and its commander was then transferred. Yesterday, large portions of the division were scattered on maneuvers in widely separated parts of northern Spain.

A contingent of the division's military police seen entering the parliament building during the night is reported by Spanish state radio to have rallied to Tejero's rebels. The major commanding the unit has been arrested, and other arrests of officers implicated in the plot are expected soon, the radio said.

A sign that the plot would fall apart came early in the night when an Army captain who took over the state television and radio broadcast house for nearly two hours left, reportedly after making several telephone calls that apparently convinced him there was not enough steam behind the military movement.

During the time he and his men were there, the state media broadcast martial music. Afterward, the building was surrounded by the loyal elite Special Forces, and the king was free to broadcast his appeal later in the night.

Shortly after Tejero's surrender, a band of about 1,000 right-wing youths went on a rampage in central Madrid, blocking streets, bashing in cars and chanting pro-Tejero slogans. Police soon dispersed them.

The king met in turn today with each of the major released political leaders -- first with outgoing premier Suarez and his key ministers and then with Fraga, Carrillo and Socialist Party leader Felipe Gonzalez.

Aside from demonstrating with overwhelming clarity that the king will not go along with those in the military who still think he shares their nostalgia for the old, antidemocratic order, the hostage-taking seems to have underlined the failure of the forces dedicated to the transition to democracy to support each other sufficiently against their neofascist and military adversaries.

Many observers seem to believe, however, that the worst crisis since Franco's death may lead to a genuine catharsis for the nation.

The conservative newspaper ABC called for an approach that overcomes narrow party interests. And the conservative Catholic paper Ya editorialized: "As Spaniards, we are ashamed of our country and ourselves. . . . From now on, nothing will ever be the same again. It will take a long time to erase, if that is possible, the marks of what has happened."