An underground fire Sunday night near a subway station in Midtown Manhattan caused a cascade of delays, detours and station closings well into Monday.

Service on the affected lines — mostly the D, E and F — was restored by 12:30 p.m., according to the Metropolitan Transportation Authority.

The fire began in the basement of an electrical substation for the transit system near the Seventh Avenue station after 10 p.m. Sunday, Fire Department officials said. The fire knocked out power to the third rail, a transit official said. The subway station filled with smoke and about 1,000 people were evacuated, a Fire Department spokesman said.

Con Edison workers also detected a gas leak in the area; the connection between the leak and the fire was not immediately clear.