Service on the Q and R lines was restored Thursday evening after a water main break near the Court Street subway station snarled service for several hours, the MTA said.

The water main break, located outside 158 Montague St. in Brooklyn Heights, sent water rushing into the Court Street station just before 4 p.m., city officials said.

Members of the city's Department of Environmental Protection were able to shut off water to the 12-inch pipe that ruptured and repairs were being made, a spokesman said.

As a result, some R trains were rerouted via the Q line in both directions between Canal Street in Manhattan and DeKalb Avenue in Brooklyn. Additionally, some southbound R trains were terminated at City Hall or Whitehall Street-South Ferry.

MTA crews needed to evacuate the water, which appeared to be several inches high, before a track team could begin testing equipment and infrastructure, including signals and switches. 

The MTA ran a test train through the station around 8 p.m. and service was restored around 8:30 p.m., more than four hours after the station flooded.