Two Boston firefighters died Wednesday while battling a wind-whipped blaze that tore through a Back Bay apartment building, causing a fiery explosion that sent people screaming into the streets and trapped the firefighters in the basement, where they desperately called for help as their water ran low.

Fanned by winds gusting up to 45 miles an hour, the nine-alarm fire blasted up from the basement of the four-story apartment building, triggering a powerful burst that ultimately took the lives of Lieutenant Edward J. Walsh Jr. and Firefighter Michael R. Kennedy, despite frantic attempts to save them.

Authorities said the cause of the fire at 298 Beacon St. remained under investigation, but that the wind blowing off the Charles River caused it to rage out of control within minutes, engulfing the neighborhood in a thick cloud of black smoke.

“The fire escalated very quickly,” Deputy Fire Chief Joseph Finn said at a press briefing Wednesday night. “In 30 years, I’ve never seen a fire travel that fast, escalate that quickly, and cause such havoc.”

The fire, which broke out just before 3 p.m., was so dangerous that firefighters could not recover Walsh’s body until nearly 8 p.m.

Fire officials knew that Walsh remained in the building but could not get to him as the fire raged, Finn said, calling it a “tough call” but a necessary one.

Walsh, who had been on the force for more than nine years, was taken out of the building on a stretcher through a line of firefighters who stood at attention.

“Everyone saluted him, and Eddie was taken for his last ride,” said Steve MacDonald, a Boston Fire Department spokesman.

Kennedy was pulled from the basement about a half-hour after the fire broke out and was rushed to the hospital, where he was pronounced dead. Thirteen other firefighters were injured, some with burns and broken bones. Their injuries were not believed to be life-threatening.

It was the first time Boston firefighters had died battling a fire since 2007, when two firefighters were killed in a blaze at a West Roxbury restaurant. In 2009, Lieutenant Kevin M. Kelley was killed when a ladder truck lost its brakes and crashed into a Mission Hill apartment building.

The Beacon Street fire was just blocks away from a 1972 fire at the Hotel Vendome, which killed nine firefighters.

On Wednesday, Walsh, a 43-year-old married father of three young children, and Kennedy, a 33-year-old Marine Corps veteran, had made their way down stairs to the basement when a front window broke, intensifying the blaze, authorities said. Just minutes after entering the building, they were trapped in the cellar, prompting a frantic mayday call.

“We’re in the basement,” one of the men said. “Please come and get us.”

After the seventh alarm sounded, all firefighters were ordered from the building through a haze of screams and sirens. But when word came that some firefighters were missing, some vowed to go back in.

“No companies should be going in anywhere; stay away from the building,” firefighters were instructed in the mayday call.

“We are aware of the potential we see in front of us; we’re going back inside the building,” came the reply.

But the firefighters were told, “Stay out of the building.”

Walsh and Kennedy’s actions allowed firefighters to rescue a number of residents from the upper floors of the apartment building, authorities said.

“We lost two heroes here today,” Mayor Martin J. Walsh said at the press conference. “These two heroes ran into a burning building and got people out.”

In a statement, the mayor said words “cannot do justice to the grief that we feel tonight.”

“Our hearts are heavy with the knowledge that these brave men gave their lives to protect the safety of our city and its people,” he said. “A day like today makes us all too aware of what they are risking in the course of doing their jobs. They are heroes simply by virtue of accepting this duty.”

‘We lost two heroes here today,’ Walsh says http://brightcove.vo.llnwd.net/d21/unsecured/media/245991542/201403/253/245991542_3396829177001_Sequence-1.mp4?pubId=245991542&videoId=3396758687001 Lt. Edward J. Walsh and Firefighter Michael R. Kennedy were killed in the ferocious blaze, officials said at a news conference.

Governor Deval Patrick said the tragedy “reinforces how we must be grateful every single day for the brave men and women who put themselves in danger day in and day out to keep us safe.”

When Walsh’s body was finally removed from the building, the firefighters he worked with carried him out, said MacDonald. Both men worked out of the firehouse on Boylston Street, near the scene of the smoky blaze.

“They [worked] in one of the busiest firehouses in the city, and that’s by choice,” said MacDonald.

The building, which has eight units, was destroyed in Wednesday’s fire. Authorities did not say how many people were displaced, but the property manager said all residents were accounted for, fire officials said.

Finn said he had no reason to suspect criminal intent.

The building has no history of significant problems, according to public records.

City and land records say the property is owned by Michael J. Callahan. Both of his daughters said that he is dead and that the property is controlled by his estate, in which they do not have a role.

Elaine Callahan said the property is owned and managed by the estate of her late father and was being rented. She said she has been battling for years to gain control of the property and last lived there in 1996.

Callahan said there is nothing about the building’s structure that would cause it to be consumed by fire so quickly.

“It’s unfathomable that this should have gone up,” she said.

Sam Wallace, who lives nearby, said he saw an explosion after the building caught fire.

“People were running out of the building screaming,” said Wallace, president of the Neighborhood Association of the Back Bay.

Wallace said he saw several firefighters, soot covering their faces, being put into ambulances.

“People were crying and weeping,” he said. “This has really been a sad day.”

James Hardy, a student at the Berklee College of Music, was in his Beacon Street apartment when the chaos broke out.

Firefighters, wearing oxygen masks, rushed up and down the street, as ladder trucks turned powerful blasts of water on the building.

“I hear all this yelling, and I look out my window,’’ Hardy said. “I could see firefighters running down the street with oxygen masks on, and cherrypickers going up and the smoke spreading everywhere.’’

When the odor of smoke reached his apartment, Hardy made a move. “I could smell the smoke, so I left,’’ he said.

Kalya DaSilvia, 19, was in her Commonwealth Avenue dormitory when she smelled smoke, even though her window was closed.

“When we first got to the fire, thick black smoke was coming from that roof. About 15 minutes later, all of a sudden, the smoke got really, really black,’’ DaSilvia said. “A huge flame came out the front door and broke the door open, broke the front windows open.’’

A short time later, she saw a Boston firefighter receiving emergency first aid from his colleagues. “They were doing compressions on him,” she said. “They were trying to get him in an ambulance. It was really chaotic. . . . The smoke was really thick.’’

Firefighters threw at least four ladders against the building and clambered up them to direct streams of water into the building. Firefighters could also be seen walking on the roofs of nearby buildings.

Around 4:30 p.m., a police officer ordered reporters and others gathered on Beacon Street to move down the street because the fire scene was now deemed a crime scene.

“We have someone badly hurt over here!’’ the officer shouted at people.

The day’s turbulent weather played a major role in the fire, with strong winds generated by a storm passing along the coast. Shortly before 3 p.m., around the time the fire was reported, winds were recorded as blowing at 32 miles per hour at Logan International Airport, gusting to 45. At around 4 p.m., winds were blowing at 28 m.p.h. and gusting to 39.

Student Kirby Paulson, 16, said the entire neighborhood was quickly “smoked out.”

“I heard people screaming: ‘Get an ambulance! Get an ambulance!’ ” he said.

“Beacon Street is filled with fire trucks and EMS vehicles,” said Tim Fitzgerald, who works at an office down the street. “It’s so windy that smoke is pouring out of the building.”

Gilad Haas, who lives in the Back Bay, said he saw flames explode from the building.

“I saw this explosion,’’ Haas said at the scene. “It was a burst, and then I saw the flames go up. It was definitely scary.”

As described in the harrowing mayday call, rescuers made their way to the back of the building to find a wall of flames, and called for medics.

“Oh, my God,” one said. “We need the fourth and fifth alarm. We need them in the rear of the building.”

Later Wednesday night, Sheila Dunning, who lives in the Back Bay and walks past the firehouse at 941 Boylston St. every day, made her way to the station to add a bouquet of flowers to a growing memorial.

“The level of heroism was incomprehensible to me,” she said later by phone. “I just wanted to pay my respects.”

A firefighter also walked up to 941 Boylston St., home to Engine 33 and Ladder 15, and placed a bouquet of flowers next to several others. He took off his Red Sox hat, placed it over his heart, and bowed his head and prayed.

A woman who said she has lived in the Back Bay for 45 years brought cakes, croissants, and cranberry bread to the firehouse. She said she prays every time the sirens sound, for God to take care of them and keep them safe.

Firefighters escorted the body of one of their fallen colleagues from Boston Medical Center. Jim Davis / Globe Staff

Evan Allen, Maria Cramer, Kevin Cullen, Todd Wallack, Laura Crimaldi, John R. Ellement, and Martin Finucane of the Globe staff, and Globe correspondents Jacqueline Tempera, Jennifer Smith, and Catalina Gaitan contributed to this report.