CAIRO — Riyad Mahrez scored from a free kick in the fifth minute of injury time as Algeria beat Nigeria 2-1 on Sunday and progressed to the African Cup of Nations final against Sadio Mane's Senegal.

Mahrez thundered his left-footed match-winner into the net in effectively the last kick of the game to stunningly settle the semifinal at Cairo International Stadium.

Algeria will play for a second African title and first in nearly 30 years in Friday's final. Senegal goes for its first African Cup title after 54 years of trying.

The teams met in the group stage at this tournament when Algeria won 1-0.

The final will also feature an intriguing Premier League subplot as Manchester City's Mahrez comes up against Liverpool's Mane.

Senegal went through after beating Tunisia 1-0 in an extra-time thriller at the 30 June Stadium across Cairo.

Both semifinals had dramatic VAR moments, with the referee video review system being used for the first time at the African Cup this year. The referees made good use of it in the semis, with two drawn-out decisions.

Algeria led Nigeria through a first-half own-goal by William Troost-Ekong. Mahrez's cross deflected off another Nigerian defender, then hit Troost-Ekong in the midriff and went in.

Algeria was pegged back when Nigeria was given a penalty for handball after a long VAR referral by Gambian referee Bakary Gassama, who initially didn't award the spot-kick. A shot by Oghenekaro Etebo hit his own teammate Odion Ighalo and the arm of Algeria defender Aissa Mandi at just about the same time. Gassama didn't give it at first, then referred to the TV screen on the sidelines more than a minute later and went back for the penalty.

Nigeria took its opportunity — contentious as it was — to level at 1-1 from the spot through Ighalo.

But Algeria captain Mahrez won it at the very death, hammering his free kick into the net and sprinting half the length of the field to celebrate with teammates on the bench.

"This free kick arrived and, with the quality of a player like Mahrez, it's a massive chance at a goal," Algeria coach Djamel Belmadi said. "Thank God we made it."

There were tense moments off the field at Cairo International also as Algerian fans and local Egyptian spectators began throwing plastic bottles at each other over a fence that separated them. Some of the Egyptians had started to cheer for Nigeria over Egypt's North African rivals.

Lines of yellow vested security personnel were brought into the stands to stand between the supporters.

Senegal won the first semifinal with an own-goal in the 100th minute when Tunisia goalkeeper Mouez Hassen pushed a free kick onto the head of defender Dylan Bronn and the ball bounced back into the goal.

It was another game of high drama.

On a day when the sports world was treated to epic contests at the Wimbledon men's tennis final and the Cricket World Cup final, the African Cup held up its end of the bargain.

Senegal and Tunisia both missed penalties within a few minutes of each other in regulation time.

Tunisia was then given another penalty late in extra time only for referee Bamlak Tessema Weyesa to check the VAR TV screen on the sidelines and dramatically reverse his decision — to the dismay of the Tunisians.

Tunisia should have gone ahead after winning the first penalty in the 73rd, when Ferjani Sassi's shot hit the upper arm of Senegal defender Kalidou Koulibaly as he threw himself in the way to block it.

Sassi took the penalty himself but Senegal goalkeeper Alfred Gomis saved easily.

Senegal had its chance almost straight after, with Ismaila Sarr hacked down in the area.

Henri Saivet took the spot-kick instead of Mane, who missed two penalties earlier in the tournament. His penalty was low and hard to the bottom right corner, but Hassen dived full-stretch and brilliantly deflected it away with his left hand.

Hassen was responsible for deciding Tunisia's fate, though.

In extra time, he went to punch away a free kick but only forced it onto Bronn's head and it rebounded into the net.

The most contentious moment came right near the end after Senegal's Idrissa Gueye was initially penalized for another handball in the area. The ball was headed down by a teammate and struck Gueye's hand as he tried to pull it away at the side of his body.

Ethiopian referee Weyesa awarded the penalty, then decided to make the long run over to the sidelines to check with the VAR.

After another long delay, he ran back onto the field waving his arms to signal no penalty.

Senegal has made it to just one final before, in 2002. Current Senegal coach Aliou Cisse was a member of that 2002 team and he dropped to his knees with arms held aloft in celebration at the final whistle.

___

More AP soccer: https://apnews.com/apf-Soccer and https://twitter.com/AP_Sports