Updated

Montreal, QC (SportsNetwork.com) - Daniel Riccardo and Red Bull pulled off a stunning upset over Nico Rosberg and Mercedes to win Sunday's Canadian Grand Prix, a race that ended in thrilling fashion.

Rosberg started on the pole and had been dominant in this 70-lap race at Montreal's Circuit Gilles Villeneuve, but Ricciardo overtook Rosberg with less than three laps to go.

The race ended with the deployment of the safety car when Force India's Sergio Perez and Felipe Massa from Williams made contact and spun out before both drivers slammed hard into the barrier on the final lap. They were battling for position in the top-five at the time of their crash.

The top-five were running within a couple of seconds from each other heading into the last lap around this 2.71-mile (4.361-kilometer), 14-turn circuit.

Ricciardo scored his maiden victory in Formula One, coming in his 57th career start. He became the 105th different driver to win a F1 grand prix and first new winner since 2012 when Pastor Maldonado took the checkered flag for the Spanish Grand Prix.

"It's still a bit surreal but yeah, just really cool," Ricciardo said. "It's not that we were leading the whole race, so it's not that I had time to understand that I was going to win. It all happened in the last few laps, so I think that's why it's still taking a while to comprehend in my head. But really nice, a really good feeling."

Ricciardo also became the fourth Australian to reach the top step on the podium in F1, joining Jack Brabham, Alan Jones and Mark Webber. The 24-year- old Ricciardo is in his first season driving for Red Bull. He took over Webber's ride when Webber retired from F1 at the end of the 2013 season.

"An amazing race today and what an amazing race for Daniel to win his first grand prix," Red Bull team principal Christian Horner said. "He's been phenomenal since he joined the team, and I think today he fully deserved the victory."

Rosberg, who had won the Monaco Grand Prix for the second year in a row two weeks ago, dealt with an ill-handling car in the closing laps. The German held on for a second-place finish.

"What happened was that I lost the ERS [Energy Recovery System], and when you lose ERS, then it doesn't harvest anymore and then all the braking on the rear is being done by the brakes and then the rear brakes overheated," Rosberg said. "So it was one problem and then the next problem happened. That just made it massively difficult. I needed to cool the brakes a lot. I lost a lot of power on the straights."

It was the first time this season that Rosberg or his teammate, Lewis Hamilton, had not won a grand prix this season. Mercedes had been undefeated for the first six rounds.

Hamilton, who started second, had been battling Rosberg for the lead for a majority of this race. After making his second and final pit stop, Hamilton came out ahead of Rosberg.

Moments later, Hamilton ran wide at the hairpin and lost the position to Rosberg. Hamilton attempted to reclaim it at the final chicane but ran off track and damaged his brakes in the process, forcing him to retire.

"I did the best that I could out there today and felt like I was having a strong race, but unfortunately it just wasn't our day," Hamilton said. "Montreal has been a good track for me, so to come here and not finish is disappointing, but there are plenty more races ahead of us this season, so let's hope for better fortune."

Riccardo's teammate, Sebastian Vettel, the four-time and defending Formula One world champion, crossed the line in third. Vettel won this race for the first time one year ago.

"Congratulations to Daniel for his first win, which is very, very special," Vettel said. "I'm happy for him and happy for the team. It's nice to be able to finally break through to get a win this year and capitalize on Mercedes having a problem."

Neither Perez nor Massa was injured in their last-lap crash.

"On the final lap, I was defending my position going into turn 1 when I suddenly got hit from behind," Perez said. "It was a big impact, but I am okay. I'm really sad for the team, because we had an amazing race today, and the one-stop strategy was working perfectly."

Perez received a five-place grid penalty for the next round, the June 22 Austrian Grand Prix, for triggering the accident with Massa. Race stewards determined that Perez had changed his racing lane, which caused the impact.

Rosberg increased his points lead to 22 over Hamilton (140-118). Ricciardo moved up to third in the world championship standings (-61).