NYCFC (11-4-5, 38 points) remained in second place in the Eastern Conference and moved five points ahead of the Red Bulls (9-6-6, 33 points). The defending MLS Cup champions drew within one point of conference leader Philadelphia.
Castellanos helped NYCFC get its third straight win when he slipped by Red Bulls defender Sean Nealis in the middle of the box. Castellanos got a long pass from Santiago Rodriguez and finished off the sequence by putting a right-footed shot into the center of the net.
It was the 23-year-old forward's 13th goal of the season and his sixth over the past four games.
NYCFC goalie Sean Johnson made four saves for his league-leading 10th shutout. Johnson matched his career high set in 2018 and also notched his 86th career shutout.
The Red Bulls saw a six-game home unbeaten streak (4-0-2) against NYCFC snapped. New York also had a three-game unbeaten streak stopped and was unable to rally for its fourth straight home victory.
New York nearly tied the game in the final minutes, but Cameron Harper missed the net and Johnson denied Nealis.
NYCFC had ample chances to get the lead in the final moments of the first half. Rodriguez's right-footed shot was saved in the 44th minute -- five minutes after Castellanos missed the net.