Investigators were trying to determine if the shooting was connected to a botched robbery.
Responding officers encountered the suspect outside the San Francisco Giftcenter & Jewelrymart in the trendy South of Market area. At first they were not sure if the man, who had blood on his clothes, was a shooting victim or a suspect, police Chief Greg Suhr said.
The man, whose name was not released, opened fire at officers while retreating into a restaurant, Suhr said. The man kept shooting until he apparently ran out of ammunition and surrendered, Suhr said.
Officers did not return fire because the sidewalks were crowded with shoppers and residents.
The suspect was treated at a hospital for non-life-threatening injuries. A gun and a knife were recovered from the scene.
SWAT officers swept the building, but police said there were no outstanding suspects.
"We believe from video in an adjacent store that there was one suspect. We believe that it is the suspect we have in custody," Suhr said.
The man arrested had been in that area before, according to witnesses.
After the arrest, officers went inside the shopping center and found the two women dead in the lobby.
Another man with life-threatening gunshot and stab wounds was taken to a hospital, police said.
The shopping center is just blocks from the Hall of Justice, which is the home of San Francisco Superior Court, a jail and police headquarters. Several tech companies, including online game maker Zynga, are also in the neighborhood.
Officers were interviewing witnesses who ducked for cover when the shooting began.
"Somebody was crying or yelling for help. Then I saw the SWAT. There was a dozen of them. They told us to lock ourselves in," Veronica Sirois, who was shopping at the gift center, told the San Francisco Chronicle. "I saw blood on the way out and a pair of bodies."
AP Video Journalist Haven Daley contributed.