Terrorists bombed a building that houses offices of Trans World Airlines and British Airways and attacked the nearby Alia Royal Jordanian Airline today, killing an elderly Spanish woman and injuring 27 persons, among them an American teen-ager on vacation.

Police said they believe the intended target of the bomb damaging the British Airways office was TWA. The bomb exploded on the street level, where British Airways has a sales office, while the floor above is occupied by TWA. The U.S. airline's large red sign dominates the facade.

In Beirut, an anonymous caller said a group carried out the attack in reply to President Reagan's threat to strike against terrorists. Reuter quoted the caller as saying, "Let Reagan know that our hands will reach the whole world . . . . We thank all the alliances which helped us to carry out the bombing of the TWA office -- Organization of the Oppressed." Hijackers holding the TWA airliner at Beirut signed a statement made to reporters Sunday "The Oppressed of the Earth."

Sidney Bridges, 17, from Ontario, Calif., was cut in the face and arms by the flying glass in the British Airways blast. According to his older brother, Don, Sidney Bridges saw the bomber.

Twenty-five were injured in the parcel-bomb blast, which gutted the British Airways sales room and killed the woman, who was buying a ticket. Two severely injured were British Airways employes.

Five minutes after the bomb exploded at British Airways in the busy commercial center of Madrid, gunmen raked the Alia offices 200 yards away with automatic-weapons fire and lobbed two grenades through the shattered plate glass..

The grenades failed to explode and later were deactivated by police. Two persons in the Jordanian office were hurt by flying glass.

A connecting thread between Madrid and the U.S. hostages in Beirut was formed by the trial last month in the Spanish capital of two radical Shiite Moslems.

The TWA plane hijackers had at one time demanded their release in return for the freedom of the hostages, but the Spanish government, according to Prime Minister Felipe Gonzalez, signaled that it "would not bow to terrorist blackmail" and the demand was dropped.

The two Shiites were sentenced last week to 23 years in jail for the wounding last September of a Libyan diplomat here.

The Jordanian ambassador in Madrid said after the attack on the Alia office that terrorism would "never deter" Middle East peace initiatives undertaken by King Hussein and the Jordanian government.

The two attacks brought Madrid to a standstill. Police sealed off the area for three hours in an apparently fruitless search for the assailants.

Two other attacks last year involved Arabs on the south coast, near Marbella, which lately has become a favored haunt for the wealthy from the Persian Gulf.

Earlier this year, 18 Spaniards were killed in a bomb attack on a restaurant favored by U.S. airmen from a nearby base. The attack was claimed by Islamic Jihad in a communique from Beirut. The bombers have not been caught.