The Union pulled off a stunning late comeback to win, 2-1, at the San Jose Earthquakes in a game that ended well after 1 a.m. Eastern time Thursday.

Alejandro Bedoya and Kacper Przybylko scored in the 70th and 78th minutes, respectively, then watched the video replay booth save the win in the final minutes by overturning a penalty kick.

The win vaulted the Union (16-9-7, 55 points) into second place in the Eastern Conference, one point ahead of Atlanta United, which lost 4-1 at first-place New York City FC on Wednesday. And by the slimmest of margins, the Union still have a shot to take first on the last day of the season. If they win at Columbus on Sunday and New York loses at New England, the Union-NYCFC game on Oct. 6 will be for first place.

Jackson Yueill opened the scoring in the 35th minute for San Jose (13-14-5, 44 points). At halftime, the Union seemed to have long odds of offering a response, despite arguably their strongest attacking quartet leading the lineup: Jamiro Monteiro, Marco Fabián, Kacper Przybylko and Andrew Wooten. It was Fabián’s first start since Aug. 31 and Wooten’s first start since Aug. 4.

Yet somehow, in the second half, the game turned on its head.

Magnus Eriksson appeared to make it 2-0 just 45 seconds into the second half, but the goal was overturned by the video replay booth because Danny Hoesen was judged to be offside in the buildup. It was a close call, and the Union were lucky it went their way.

Ilsinho replaced Wooten in the 54th, and Fafa Picault replaced Fabián in the 63rd. Five minutes later, up the Pacific coast in Portland, the New England Revolution stole a 2-2 tie at the Timbers, eliminating the Columbus Crew — whom the Union will visit Sunday — from playoff contention.

Then came the Union’s goals: Alejandro Bedoya in the 70th, fed by Ray Gaddis, and Przybylko in the 78th with a towering header of a cross from Monteiro.

But that wasn’t all. Far from it, in fact. In the 81st, Aurélien Collin leaned into San Jose’s Andrés Rios in the Union’s 18-yard box. Referee Baldomero Toledo whistled for a penalty kick. But the video replay booth wasn’t convinced, and Toledo reversed the call after watching the play on the sideline monitor.

Only then could the Union finally see the game out.

The Union probably still have to win both games left to hold onto second place, never mind to aim at first. But they at least have a shot, and the long flight back east Thursday will probably go a lot more smoothly because of it.

MLS Eastern Conference standings

All teams have played 32 games. The first tiebreaker is total wins, followed by goal difference. The top four teams open the playoffs at home, and the first-place finisher gets a first-round bye)

1. New York City FC: 17-5-10, 61 points (+22 goal difference; clinched first-round home game)

2. Philadelphia Union: 16-9-7, 55 points (+11; clinched first-round home game)

3. Atlanta United: 17-12-3, 54 points (+13; clinched first-round home game)

4. D.C. United: 13-10-9, 48 points (+4; clinched playoff berth)

5. New York Red Bulls: 14-13-5, 47 points (+5; clinched playoff berth)

6. Toronto FC: 12-10-10, 46 points (+4; clinched playoff berth)

7. New England Revolution: 10-10-12, 42 points (-7)

8. Chicago Fire: 9-12-11, 38 points (+5)

9. Montreal Impact: 11-17-4, 37 points (-16)

10. Orlando City: 9-14-9, 36 points (-5)

11. Columbus Crew: 9-15-8, 35 points (eliminated)

12. FC Cincinnati: 6-22-4, 22 points (eliminated)

Games to watch

Sunday, Sept. 29

All of the games below games kick off at 5:25 p.m.

New England Revolution vs. New York City FC (ESPN+)

Columbus Crew vs. Philadelphia Union (PHL17)

Montreal Impact vs. Atlanta United (ESPN+)

New York Red Bulls vs. D.C. United (Fox Sports 1, Fox Deportes)

Chicago Fire vs. Toronto FC (ESPN+)

FC Cincinnati vs. Orlando City (ESPN+)

Sunday, Oct. 6

All games leaguewide will have the same TV window, with kickoffs at around 4 p.m. ESPN and ESPN Deportes will have national coverage, with specific plans still to be announced.

Philadelphia Union vs. New York City FC (PHL17)

Atlanta United vs. New England Revolution (ESPN+)

D.C. United vs. FC Cincinnati (ESPN+)

Montreal Impact vs. New York Red Bulls (ESPN+)

Toronto FC vs. Columbus Crew (ESPN+)

Orlando City vs. Chicago Fire (ESPN+)