A blaze broke out and quickly spread between two apartment buildings in the Mission District on Thursday morning, sending smoke and flames through the roofs and prompting evacuations of residents.

The first reports of the fire came just after 8:30 a.m. at an apartment complex on the 3500 block of 17th Street near Guerrero Street, said Jonathan Baxter, a spokesman for the San Francisco Fire Department.

Crews found a large fire in the rear of the building, and investigators suspect the blaze started between the buildings at 3517 and 3525 17th St. — three-story and four-story structures, respectively— and quickly spread to the rear of both buildings.

Residents of 22 apartment units were displaced by the blaze, officials said.

Lisa Geduldig, 54, was in bed in her apartment when she smelled smoke and opened a window to see what was going on. The odor grew stronger and she spotted flames from another window before calling 911 and pulling the building’s fire alarm, which woke a number of other residents.

Caitlyn Toombs was one of those awakened by the alarm and rushed out in her bare feet.

“I was asleep and didn’t even have time to get dressed. I didn’t even have time to put shoes on,” said Toombs, a 29-year-old MBA student at UC Berkeley who stood outside the apartment building in borrowed slippers. “It’s like your greatest fear. All my stuff is in there and it’s just stuff, but it’s all my stuff.”

David Dobson, 53, who lives on the first floor of 3525 17th St., said his rushed exit was made more complicated by his prosthetic leg.

“It all happened at once. I had to get out of the way, but I didn’t know where to go and I was slowed down by this,” he said, pointing to his prosthesis.

The fire was brought under control around 9:45 a.m.

Baxter said the density of the block, which is packed with residential apartment buildings, made the fire harder to fight and increased its potential to spread. At one point, firefighters were battling flames on all four floors in one of the buildings and two out of three in the other.

He commended the efforts of the Fire Department for keeping the damage to two buildings.

“As always in San Francisco, we have buildings that are neck and neck,” Baxter said, noting how tightly packed that block is. “They are basically stacked on top of each other.”

No injuries were reported, and the cause of the blaze was under investigation.

Kimberly Veklerov and Kale Williams are San Francisco Chronicle staff writers. Email: kveklerov@sfchronicle.com, kwilliams@sfchronicle.com Twitter: @kvekerov, @sfkale