"Thanks to the brave men and women of the NYPD and FBI, we can report tonight that a possible terror attack has been foiled," the mayor told reporters.
The FBI assistant director in charge, William Sweeney, identified the suspects as Christian and Tyler Toro, adding there appeared to be no other people involved.
A search at his apartment last week led to the discovery of more cues that an attack was being prepared at a school, including a diary that mentioned an operation "Flash," the FBI official said, adding there was no indication of remaining threat to the city.
Successful exposure of the plot came after Wednesday's tragedy in Marjory Stoneman Douglas High School in Parkland, Florida. A 19-year-old shot and killed 17 people and wounded at least 14 others with an assault rifle.