The airline didn't release a death toll, but said the plane carried 172 passengers and crew.
As smoke billowed from the wreckage, dozens of fire trucks and ambulances rushed to help, lining a nearby road and filling a field next to a swath of charred vegetation. Helicopters flew over dumping water on fires.
"The scene is devastating," said Pablo Albella, an emergency rescue worker. "The fuselage is destroyed. The plane burned. I have seen a kilometer of charred land and few whole pieces of the fuselage. It is all destruction."
Rescuers rushed the few survivors to hospitals, while emergency workers shrouded the dead in white sheets. One body lay on burned grass, an arm and a leg poking out.
Later, a long convoy of black hearses rolled onto the airport grounds to carry bodies to a makeshift morgue set up at Madrid's main convention center. Flight departures resumed after several hours.
It was not immediately clear what went wrong. Alvarez said the jetliner had barely gotten airborne when it veered right, crashed and broke into pieces.
A spokesman for Spanair, a Spanish company wholly owned by Scandinavian Airlines, said it did not know what caused the accident. Alvarez, the government minister, said investigators ruled out foul play and considered the crash an accident.
While preparing for a first takeoff attempt, the plane's pilot reported a breakdown in a gauge that measures temperature outside the plane. The gauge was fixed, delaying the departure, said Spanair spokeswoman Susana Vergara.
It was on the second takeoff attempt that the plane crashed. The Spanair spokesman, Sergio Allard, originally said Flight JK5022 to Las Palmas had 175 passengers and crew aboard, but the airline later put the number at 172. The flight, which originated in Barcelona, was a code-share with Flight LH255 of the German carrier Lufthansa.
Allard declined to give any numbers on the nationalities of those on board, saying relatives had to be notified first.
In Germany, Lufthansa said it issued tickets to seven people who checked in for the flight and four of those were from Germany. It was unclear whether they were German citizens.
Sweden's Foreign Ministry said two Swedes were on the plane. It said one was at a hospital but the other was unaccounted for.
The accident was Spain's worst air disaster since 1983, when a Boeing 747 operated by the Colombian airline Avianca crashed near Madrid on landing approach, killing 181 people. In 1985, an Iberia Boeing 727 crashed near Bilbao in the Basque region, killing 148 people.
The deadliest disaster in aviation history occurred in Spain in 1977. Two fully loaded Boeing 747s collided on a runway in the Canary Islands and a total of 583 people died.
After being informed of Wednesday's crash, Prime Minister Jose Luis Rodriguez Zapatero broke off his vacation in southern Spain and rushed back to Madrid, heading straight for the airport. Britain's Queen Elizabeth II sent condolences to King Juan Carlos of Spain. "I was deeply saddened to learn of the dreadful loss of life in today's aircraft crash at Barajas airport in Madrid, the news of which has shocked us all," she said.
In an expression of mourning, a soccer match in Copenhagen between the national teams of Denmark and Spain started with a minute of silence and players on both teams wore black armbands. Allard, the Spanair spokesman, said the crashed plane passed an inspection in January and no problems had been reported since then. The plane was 15 years old and has been owned by Spanair the past nine, he said.
The DC-9/MD-80 family of twin-engine, medium-range jets enjoyed wide popularity among the world's airlines in the 1970s, '80s and '90s.
But it has had a number of fatal accidents, the deadliest of which was a crash of Slovenia's Adria Airways flight in Corsica in 1981, when all 180 people on board perished.
In Copenhagen, Mats Jansson, the chief executive of Spanair's owner, Scandinavian Airlines, said he had no information about the accident itself.
Jansson and his deputy, John Dueholm, were on their way home from the Beijing Olympics when they learned about the accident and immediately decided to fly to Madrid with a team of crisis counselors.
"We want to be on location ... there are many questions," Jansson said before heading to Spain.
Spanair has a fleet of more than 60 aircraft and runs around 600 flights daily.
Crashes during takeoff or landing are some of the most common aviation accidents.
In July 2007, 199 people were killed in Brazil's worst air disaster when an Airbus A320 belonging to TAM airlines skidded off the runway at Sao Paolo's Congonhas airport and smashed across a road into a gas station and an air cargo building.
Five people died and 65 were injured May 30 when an A320 belonging to Grupo Taca skidded off the end of the runway at Toncontin International Airport near the Honduran capital of Tegucigalpa.