Alexander Callens scored the first goal before Castellanos converted on a penalty kick as NYCFC did all the scoring in the first half. Castellanos has seven goals this season, all coming in a five-game span.
Goalkeeper Sean Johnson made one save for NYCFC (6-3-2, 20 points), which has recorded back-to-back 2-0 wins. He has four consecutive shutouts.
Johnson's save came in the 76th minute. D.C.'s Steven Birnbaum charged through the box for a header off Julian Gressel's corner kick. Johnson reacted just in time by punching the ball out of harm's way.
Home goalkeeper Rafael Romo made four saves as United was held to its fourth shutout in MLS play this season.
Callens opened the scoring in the sixth minute with his header off Christopher Gloster's delivery following a corner kick. Romo got a hand on the ball, but the shot went into the net anyway as Callens notched his second goal of the season.
That set piece came off the game's first corner kick, though it capped a flurry of activity in the opening minutes. In the second minute, D.C. United's Brad Smith had a shot go off the right post.
Castellanos produced the second goal in first-half stoppage time on a penalty kick. That resulted from a hand-ball on D.C. United's Brendan Hines-Ike.
Castellanos' penalty kick came with a hesitation move, with Romo guessing wrong and having no chance at making a stop.
Gloster was injured midway through the second half and replaced by substitute Malte Amundsen.
NYCFC was credited with 12 of the game's 20 shots.