NEW YORK — Union Rags picked up where I’ll Have Another left off — coming from behind to catch a Bob Baffert-trained horse at the finish in a Triple Crown race.

In Saturday’s Belmont Stakes, it was even a photo finish.

Union Rags rallied through an opening on the rail to edge Paynter by a neck, dealing Baffert a third loss in this year’s Triple Crown series.

I’ll Have Another won the Kentucky Derby and Preakness with stirring stretch drives over Baffert’s Bodemeister. But the champion stunned the racing world Friday when he was scratched from the Belmont and retired due to a tendon injury, relinquishing a shot at the first Triple Crown sweep since 1978 and only the 12th ever.

His absence opened up the race for Union Rags, who finished a troubled seventh in the Derby.

A crowd of 85,811 cheered as Paynter and Union Rags battled down the stretch, and Union Rags barely caught the front-runner at the end to win by a neck.

Trained by Michael Matz, Union Rags skipped the Preakness and switched jockeys for the Belmont — from Julien Leparoux to John Velazquez, who picked up his second Belmont victory; he won in 2007 with filly Rags to Riches.

“I have to give it to the horse. He did it all for me. He just worked so unbelievable and I was just hoping he could put that work into today’s race and he did,” Velazquez said. “I was very proud of him.’”

Union Rags was along the inside in the middle of the pack until it was time to make a move for the lead. Velazquez guided Union Rags to the inside of the front-running Paynter and relentlessly closed the gap and won by a neck.

The 5-2 second choice behind Dullahan, Union Rags covered the 1 1/2 miles in 2:30.42. The colt owned by Phyllis Wyeth returned $7.50, $4.20 and $3.40. Paynter paid $5.10 and $3.90. Atigun was third and paid $10.60.

“It was my dream and he made it come true,” said Wyeth, who uses a wheelchair as the result of a 1962 car accident in which she broke her neck. “Nobody would have gotten through on the rail other than Johnny. That was unbelievable. He just said, ‘Move over, I’m coming.’ He believed in the horse and Michael got him there.”

Paynter and jockey Mike Smith bolted to the lead out of the gate and stayed in front under a moderate pace, with long shots Unstoppable U and Optimizer tucked behind him. Union Rags saved ground by hugging the rail all the way around, while Dullhan dropped back to ninth in the 11-horse field.

Turning for home, Union Rags was full of run but needed an opening. Velazquez had no room to swing outside, so he focused on finding a hole along the rail.

It wasn’t clear that the opening would ever materialize.

But Paynter slid off the rail enough to let Union Rags through in the final sixteenth of a mile. And then it was a charge to the finish line.

Union Rags and Paynter raced head-to-head, with both jockeys furiously whipping their horses in the shadow of the wire. Union Rags stuck a neck in front in a finish that was decided by a photo.

“He ran a great, great race, but I’m not too proud of my performance, though,” said Smith, a 46-year-old Hall of Fame jockey who was aboard Bodemeister in the two earlier defeats. “I’m an old veteran, you know. They’re not supposed to get through on the fence on me, and he did. I dropped the ball. My fault.”