Brazil came out on top Wednesday with a late 2-1 win over Uruguay to reach the final of the Confederations Cup.

“We knew we would face difficulties,” Brazil star Neymar said.

Veteran right-back Dani Alves called it an “important step” for Brazil. “Matches like this where you suffer make a team, that is young and ambitious like ours, more mature. We draw a very positive lesson,” he said. Local hero Fred put the hosts ahead with a shot off a rebound in the 41st minute, but Uruguay striker Edinson Cavani equalised just three minutes after the break.

In the 86th minute, with the match seemingly headed to extra time, Paulinho jumped well above Uruguay’s defence to put the hosts 2-1 up and book passage to the final.

Uruguay’s Diego Forlan failed to convert a penalty in the 15th minute.

“We lost against a great team who were playing at home. We’re sorry not to reach the final, but I think we played a good match,” Uruguay coach Oscar Tabarez said.

The match took place as clashes raged not far from the stadium between police and demonstrators, who threw stones and vandalized stores. Police fired tear gas in the melee.

The first half was full of sloppy play and losses of ball possession.

Uruguay had the first clear chance when David Luiz brought down captain Diego Lugano inside the box, and the referee pointed to the spot. Veteran striker Forlan wasted the chance with a low, soft shot deflected to corner by man-of-the-match Julio Cesar.

Just two minutes later, Brazil midfielder Oscar tried his luck from afar but shot wide.

Brazil had their clearest chance in the 28th minute. Rising star Neymar, whom Uruguay had managed to keep in check, got away and entered the box at ease, but uncharacteristically shot high and wide with only the keeper ahead of him.

Uruguay star Cavani was booked in the 21st minute for a foul on Neymar, and he should have been sent off in the 30th minute for elbowing Alves. The referee did not call the foul.

Immediately afterward, Forlan tried to make amends for his miss from the spot with a long shot, but it was a bit too wide and a bit too high.

In the 37th minute, Fred complained that he was fouled in the box as he attempted to shoot off a cross from Marcelo, but the referee did not agree. Two minutes later, Luiz Gustavo was booked after he got to the ball late and gave Cristian Rodriguez an ugly kick in the chest.

Just when it looked like the first half would end scoreless, Fred made the most of a rebound to unleash joy among the nearly 57,500 spectators in the stands of Mineirao Stadium.

Muslera blocked Neymar’s close-range shot from the left, and Fred — whose family live in Belo Horizonte and who played for two of the city’s clubs in his youth — netted the ball.

In the 48th minute, Brazil showed some poor defence in their own box, and captain Thiago Silva left the ball to Cavani as he ran down the right. The world-class striker’s left-footed shot put Uruguay 1-1.

The goal stunned Brazil and brought La Celeste back into the match. Cavani was very active down the right, and Brazil briefly lost their positional rigour as they went all out to score again.

In the 57th minute, Muslera managed to punch away a long-distance effort from Hulk, who again tried his luck with a faraway scissor kick that went far too high.

That was the last from him in the match, as coach Luiz Felipe Scolari took him off for Bernard. The crowed gave a very warm, loud welcome to the 20-year-old, the youngest player in the Brazil squad, who unlike Fred is a native of Belo Horizonte and still plays for local side Atletico Mineiro.

In the 66th minute, a header from Luis Suarez off a Forlan free kick went high, as did a header from Fred. Muslera then denied Neymar’s and Luiz Gustavo’s shots.

In the 79th minute, a mid-range shot from Cavani was deflected in the defence and went just wide.

Paulinho’s header in the 86th minute triggered a party at the Mineirao.

More In: Football | Sport