About a dozen artists facing eviction from their homes in a Mid-Market Street building would be able to stay under a tentative agreement that would have the city fund the purchase of the 15 units, which would be transferred to a nonprofit and made permanently affordable.

In a complicated deal that involved the settlement of more than half a dozen separate lawsuits, the San Francisco Mayor’s Office of Housing has agreed to finance the purchase of the loft units at 1049 Market St. for $2.4 million.

The agreement, which must be approved by the Board of Supervisors, would convert the building’s second floor into legal, below-market-rate residential units, while allowing the property owner to lease the rest of the seven-story, 56,000-square-foot structure to commercial tenants.

The 15 units will be transferred from the city to the Tenderloin Housing Clinic, which would manage the property. The city is funding the acquisition through its small sites program, which provides city loans to nonprofit organizations to purchase buildings where evictions are either pending or likely.

The deal is the latest twist in the saga of the 1907 brick former furniture warehouse, which five years ago became a symbol of how tech money was changing the Mid-Market neighborhood.

At the time, a 2012 tax break had helped lure Twitter and other tech companies to the gritty stretch of Market Street between Sixth Street and Van Ness Avenue, and investors were bidding up often-run-down commercial buildings that until then had largely housed nonprofits.

In December 2012, a limited partnership purchased 1049 Market St. for $1.7 million. The building, zoned for commercial uses, had about 80 residential tenants then — mostly artists who lived in small but bright and airy lofts with shared bathrooms.

Shortly after buying the building, the owner served the residents with eviction notices, invoking the Ellis Act, the state law that allows landlords to evict tenants in rent-controlled units if the owner wishes to exit the rental business. The law is often used to convert buildings to condominiums.

The case was more complicated because the previous owner had converted the commercial building to residential without obtaining permits, meaning that the residents were living there illegally. As tenants moved out, the owners began reconverting the units to office use, again without permits.

City Attorney Dennis Herrera said that the settlement “protects the tenants who remain, brings the building up to code and ensures that it is used legally.”

The owners “tried to kick everyone out in the middle of a housing crisis to illegally convert it back to offices and capitalize on the tech boom,” he said. “You just can’t do that.”

While the landlord successfully cleared out many of the tenants — some were paid $5,200 to relocate — a committed group of about 15 stayed and fought.

One of them, Xian Chandra Redack, a guitarist and songwriter who has been in the building since 2004, said it was the tenants — with the help of the Tenderloin Housing Clinic and the Housing Rights Committee of San Francisco — who deserved credit for the outcome. She and her building mates spent more than five years battling to hold on to their living spaces — organizing, demonstrating and speaking at public hearings.

“Nobody thought that we would make it this far — our attorney told us he just thought he was buying us a little more time,” Redack said. “The lesson here is that if you are a tenant and you fight back, you will always win something more than if you don’t fight back. You will win more time or more money or the right to stay.”

John Gall, a representative of the building ownership group, did not return a call seeking comment.

While the deal is still being finalized, rent increases for existing tenants will probably be capped at 2.5 percent a year. In order to qualify for a unit, new residents can’t earn more than 80 percent of area median income, about $66,000 for a single person. Current rents in the building range from about $600 to $1,000 a month, according to Tenderloin Housing Clinic attorney Steve Collier, who represented the tenants.

Mayor London Breed said the city had taken “a difficult situation and turned it into permanently affordable artist housing in the heart of the city.”

“As we grow and build the housing we so badly need, we also have to work to preserve the housing we already have to keep people stable in their communities,” Breed said.

Building residents include several musicians, a fashion designer, a photographer, a ballet dancer and a novelist. They are scattered throughout the building but will be consolidated on the second floor in units comparable in size to their current ones.

Redack said she loves living there — despite the proliferation of drug users outside the building in recent years. Her bright unit is filled with a dozen guitars, and it’s a quick commute to her job at Rainbow Grocery on Folsom Street.

“Rents are affordable, so I can do my creative work and have my part-time job,” she said. “There is so much going on culturally. You’ve got the theater, all kinds of musicians. You’ve got Civic Center farmers’ market, BART, Muni, City Hall. All the demonstrations, the protests, the parades. The Giants marched right by here when they won the World Series a couple of times.”

J.K. Dineen is a San Francisco Chronicle staff writer. Email: jdineen@sfchronicle.com Twitter: @sfjkdineen