Nicaragua’s Marlon Lopez pulls down Joe Corona during the first half at FirstEnergy Stadium in Cleveland. (Jay LaPrete/Agence France-Presse via Getty Images)

In many ways, the U.S. men’s national soccer team did not deserve to finish atop Group B in the CONCACAF Gold Cup. The Americans weren’t very good in the first two matches and, on Saturday against a weak opponent, they failed to convert two penalty kicks 12 minutes apart in the second half.

But they did end up winning the soft group because, with the U.S. in need of a three-goal victory over Nicaragua to claim first place, defender Matt Miazga scored in the 88th minute for a 3-0 triumph before 27,934 at Cleveland’s FirstEnergy Stadium.

Graham Zusi served a free kick deep into the box. Miazga, a 21-year-old defender under contract with English power Chelsea, ran unmarked around the side and nodded in the well-placed ball from four yards.

The Americans (2-0-1) finished tied with Panama (2-0-1) in points and goal differential but ahead on total goals (7-6) in the 12-team regional competition. They will face the third-place team from Group A or C on Wednesday at Lincoln Financial Field in Philadelphia.

Without Miazga’s late goal, the United States would have been paired with Costa Rica, the Group A winner (2-0-1). Instead, Panama will play the Ticos, also Wednesday in Philadelphia.

Before kickoff, the Americans knew exactly what they needed to do. Earlier in the day, with a 3-0 victory over Martinique, Panama turned up the pressure on the United States.

Coach Bruce Arena had planned all along to use almost everyone on the 23-man roster in the group stage, and in the last match, he swapped out every starter from Wednesday’s anxious performance against Martinique. The changes included D.C. United goalkeeper Bill Hamid, whose previous appearance came in November 2014 against Ireland.

The Americans applied early pressure but didn’t sustain rhythm, struggled to connect passes and let the tempo drop. They weren’t in danger of conceding a goal, but they weren’t exactly threatening to smash the 13th-best team in CONCACAF, according to FIFA’s rankings.

With the half winding down, Joe Corona shattered the deadlock.

Alejandro Bedoya surged on the right flank and crossed to the top of the penalty area. Corona, the late runner, took one touch forward and a pair of touches to his right to dodge two defenders. His low effort from 15 yards struck the hand of a sliding foe, Bismarck Veliz, and skidded into the left corner for his first goal since the 2013 Gold Cup against El Salvador.

Early in the second half, Dom Dwyer drew a penalty kick, then failed to convert as Justo Lorente blocked the waist-high shot targeting the left side.

Five minutes later, the Americans made no mistakes. Bedoya intercepted a pass deep in Nicaragua’s end and tried working a combination with Dwyer. The ball deflected back to Bedoya, who slid it wide to Kelyn Rowe for an eight-yard shot with his inside foot. It took a slight deflection and beat Lorente to the near corner for Rowe’s first international goal.

The Americans got another chance from the penalty spot in the 63rd minute after Dany Cadena handled the ball in the box on Bedoya’s long run. Corona’s bid, though, was worse than Dwyer’s, a low attempt too close to the middle of the net, allowing Lorente to stuff it.

Arena turned to his bench, inserting Jordan Morris (two goals vs. Martinique) and Juan Agudelo in place of the failed penalty takers.

Luis Copete received his second yellow card in the 85th minute, leaving Nicaragua with 10 men. On the ensuing free kick, Miazga scored the dramatic match-winner for his first U.S. goal.

USA:­ Hamid; Zusi, Miazga, Besler, Villafaña; McCarty; Pontius, Bedoya, Corona (Morris 66th), Rowe (Arriola 62nd); Dwyer (Agudelo 74th).