Valentin Castellano celebrates a goal for NYCFC. (Noah K. Murray-USA TODAY Sports)

That came in the form of second-half sub Valentín Castellanos, who struck twice in the final seven minutes to boost City to a 3-2 comeback win over the 10-man Houston Dynamo before 18,113 Thursday night.

City (10-4-8, 38 points) climbed into third place in the Eastern Conference past the idle Red Bulls (37) and D.C. United (36) while Houston, in ninth place in the Western Conference (30), fell to 9-12-3.

“We had to win the game. We needed the result,” NYCFC head coach Dome Torrent said after both sides endured a one-hour lightning delay at halftime.

“The most important thing tonight is the team’s fight until the last moment,” he added.

Castellanos, who replaced Eric Miller in the 58th minute, made his presence felt by connecting for the equalizing goal in the 83rd minute off an assist by Keaton Parks.

Three minutes into stoppage time, he struck for the game-winner.

“I said to Taty during halftime–10 minutes in he will play,” Torrent said. “I think Taty plays much better when he is a striker. He has this quality. Right now, I am very happy with him because everybody deserves to play but he showed me in training that he has the desire to play every single game. He is a fighter. This year he has improved a lot.”

The Dynamo played the final 61 minutes of the match a man down after Romell Quioto was red carded for violent conduct in the 29th minute.

“First, on behalf of the club and the team that I am the head coach, we apologize,” Houston head coach Wilmer Cabrera said. “Not only to the New York City FC fans, but also to the referees, and the people that came to the stadium that are fans of our club, because that behavior, we cannot tolerate that. It will not happen again because that is against the game. We came to play soccer. We did not come to fight and be superheroes or super machos. That is not what we practice and that is not what we work on. That is not the way we work in our team. It has happened, it is the second time and it will not happen again. I promise that to the league, to the referees, to the fans, to the game, because that is not right.”

Alexandru Mitrita, off a Heber feed, gave the hosts a 16th-advantage, but Houston knotted things up thanks to a Juan Cabezas goal in the 26th minute. Former NYCFC midfielder Tommy McNamara assisted on the goal.

Mauro Manotas powered the Dynamo into a 2-1 lead, off a Matias Vera assist in the 77th minute.

“It’s very disappointing, to one, go down a man so early in the game and two, to fight for whatever it is 60 minutes,” McNamara said. “To have 10 minutes left up 2-1, and to give so much, sacrifice so much and to then come away with nothing … it’s a tough pill to swallow. It wasn’t for a lack of effort, wasn’t for a lack of commitment, but it’s a tough way to end it. We have to recover well.”

City plays at Atlanta United Sunday.

“We have confidence for the next game because when that happens, you know exactly that you are able to come back,” Torrent said. “The next game is a tough game for both teams, not just New York City.”