The gunman then killed himself, the police said.
Two of the victims were brothers and members of the Yellow Dogs, a group that got its start in Tehran, rehearsing in a makeshift soundproof studio and organizing clandestine concerts to avoid punishment by Iranian authorities. The third victim was another Iranian musician, Ali Eskandarian, according to the band’s manager.
The assailant, who was not immediately identified, was believed to be another Iranian-American musician, possibly playing with the Yellow Dogs or another group called the Free Keys, said Raymond W. Kelly, the police commissioner. Mr. Kelly added that the motive had something to do with “a dispute over money involving an indie rock band.”
The Yellow Dogs’ manager, Ali Salehezadeh, said that members of the two bands had been “acquaintances in the past,” but that a “personal conflict between the guys resulted in the dissolution of their relationship in 2012.”
The gunman appeared to gain access to the three-story building, at 318 Maujer Street, from the roof, Mr. Kelly said. He fired at least once from outside the building, the bullet piercing a window and striking one of the victims. He then proceeded down into the building, shooting his victims on the second and third floors. After the shootings, the gunman went back to the roof, where he shot himself in the head with the gun, which the police said appeared to be an assault rifle.
A fourth victim, 22, was taken to Elmhurst Hospital Center with a gunshot wound to the right arm, the police said. The wounded man, who was not believed to be a band member, was identified in reports as Sasan Sadeghpourosko; hospital officials said Mr. Sadeghpourosko was treated, and then released just before 7 a.m.
At least two other people were in the home during the shooting and were being interviewed on Monday morning by detectives. It was not clear if the gunman spoke during the shooting.
A law enforcement official said that two of the shooting victims were brothers; Mr. Salehezadeh, the band’s manager, confirmed that Soroush Farazmand, a guitarist, and Arash Farazmand, the drummer, had been killed.
At least three of the band members are originally from Tehran. In a video interview last year, the bassist, identified only as Koory, said, “We were super lucky we didn’t get caught.”
The men described in the video the long process of seeking asylum in the United States and then finding themselves playing at prominent rock venues around New York, including Webster Hall, as well as at the South by Southwest music festival in Austin, Tex. “We can’t go back to Iran,” the singer, known as Obash, said. “Our parents, they have never seen us play.” Members of the group received asylum last year.
Another of the band’s guitarists, Siavash Karampour, was not involved in the shooting, according to the owner of a lounge in Morningside Heights in Manhattan, where Mr. Karampour tended bar on Sunday night. Mr. Karampour told the owner that he had spent most of the night being questioned by the police before being released.
Adam Geringer-Dunn, who books bands at Brooklyn Bowl, a popular venue in Williamsburg, said the Yellow Dogs were one of his best acts, and performed there regularly. They last played at Brooklyn Bowl on Oct. 23.
“You could count on them to consistently put on a great show and bring people out,” he said. “They were a great band that had been grinding away in New York City for years.”
Martin Greenman, 63, a recycler who works a few doors away from the apartment, said, “They were always together and they were always friendly.”
“They looked like rockers,” he said.
Their Twitter profile calls them “a post punk/dance punk band from Tehran/Iran, living in Brooklyn at the moment.”
Marcus Durant, 56, an electrician who lives just a few doors down from the band on a block of residential and industrial buildings, said he would sit on his porch, sip beer and listen to the music coming from their building. “They’re always playing music,” he said. “I sit outside and listen to them. Pretty good stuff.”
He described the ethnic mix of young people who frequented the building as a “hodgepodge.” He added that he was friendly with the band.
“They were a bunch of good kids,” he said. “You see them on their little skateboards in their tight pants.”
The men were known on their block for their long hair and tight jeans and were often seen lugging their instruments, the very picture of a young and aspiring band in an area of the city teeming with them.
They sang, partied, ate meals and skateboarded together, neighbors said.
Mr. Durant said he heard around five gunshots on Monday morning just after midnight.
“I heard a few things popping,” he said. “I thought it was fireworks.”
In the video interview, Obash said the band had made an easy transition to living in New York.
“You don’t feel like a foreigner in New York City at all,” he said. “Right now, we appreciate everything.”