Updated

Budapest, Hungary (SportsNetwork.com) - Daniel Ricciardo from Red Bull battled changing weather conditions, benefited from a pair of safety-car periods and capitalized on superb pit strategy to win Sunday's Hungarian Grand Prix.

Ricciardo overtook Ferrari's Fernando Alonso for the lead with three laps remaining and then beat Alonso to the finish line by 5.2 seconds for his second career win in Formula One. The 25-year-old Australian scored his maiden victory in the June 8 Canadian Grand Prix.

"Winning this today, it honestly feels as good as the first [in Canada]," Ricciardo said. "This one sank in a lot quicker. Crossing the line today, I knew what was going on a bit more. And I could enjoy it immediately rather than it being delayed. It was awesome. To have to pass guys again to win the race, as I did in Canada, makes it a lot more satisfying, knowing that we did have a bit of a fight on our hands."

After starting from the 22nd position and from pit lane, Lewis Hamilton charged through the field in this 70-lap grand prix to finish third, one spot ahead of his Mercedes teammate, Nico Rosberg, the current F1 championship leader.

Hamilton started from the pit lane for the first time in his F1 career after he suffered an engine fire in the early going of Saturday's qualifying at the Hungaroring. His team had to make a chassis, engine and gearbox change, which forced Hamilton to start the race from the pits. On the opening lap, Hamilton experienced a brake issue, causing him to slide off of the slippery track and into the barrier. He did not sustain any significant damage to his car during the incident.

"It is a great result today," Hamilton said. "I was just pushing as hard as possible to get as high as I could. It was obviously damage limitation after what happened yesterday. I can't express the pain I feel when we have issues such as in the last couple of races. It's hard to swallow and difficult to come back the next day and get the right balance between not attacking too much but pushing to the limit."

A heavy shower moved over this 2.722-mile (4.381-kilometer), 14-turn circuit shortly before the start of this race. The wet surface caused numerous crashes, forcing the deployment of the safety car two times.

It was deployed for the first time on lap 8 when Marcus Ericsson from Caterham lost control exiting turn 3 and slammed hard into the barrier. The final safety-car period occurred on lap 23 when Force India's Sergio Perez ran wide coming out of a corner and made heavy impact with the wall. The two periods set up various tire strategies for teams when they pitted. The strategies worked towards Ricciardo and Hamilton's favor.

The weather improved and the track surface had dried for the second half of the race.

Hamilton trimmed Rosberg's lead from 14 points to 11.

"The two safety-car periods helped quite a lot, but naturally I looked fast and had the pace this weekend, so there was an opportunity for more points," Hamilton said. "I would like to say a big congratulations to Daniel and Fernando. It was close at the end, and it was getting very hard to keep Nico behind, as he was catching at close to three seconds a lap. Coming back, through, shows how great this car and team is, and this result is much more satisfying than an easy win. Going into the break, I'm still in the fight for the championship, which is very satisfying."

Rosberg started on the pole and ran in front until the first round of pit stops during the safety-car period. The separation between race winner Ricciardo and fourth-place Rosberg was only 6.3 seconds.

"That was a disappointing afternoon," Rosberg said. "A few things didn't work out for me and it was a very up and down race. In the beginning it was all under control. Unfortunately the safety car cost me the lead, because I just missed the pit entry and then I couldn't pit. I also had some braking issues after the safety car went in and a difficult period of time with handling this, which cost me some positions."

Alonso's second-place run marked his best finish of the season. He also earned his and Ferrari's second podium this year.

"I think it has been a tough weekend, a tough season in general, so to get a podium is always a nice surprise, let's say," Alonso said. "We took a gamble. We risked today just trying to get the victory."

Felipe Massa from Williams finished fifth, while Ferrari's Kimi Raikkonen placed sixth.

Ricciardo's teammate, Sebastian Vettel, the four-time F1 world champion, finished seventh after rebounding from a spin at the halfway point. Vettel ran wide just past the entrance to pit lane and did a 360-degree slide before making slight contact with the wall.

"It wasn't a great race," Vettel said. "I spun, which was my mistake, and before that, I was unlucky with the safety car in the first stint. The timing meant the first four cars lost positions to those running behind. Unfortunately, we were then in the wrong mode for the restart, and I lost two positions. It's good for Daniel that he won today, I'm happy for him."

Valtteri Bottas (Williams), Jean-Eric Verge (Toro Rosso) and Jenson Button (McLaren) completed the top-10.

With 11 races completed, Rosberg, Hamilton and Riccardo are the only drivers who have won a grand prix this season. F1 takes a three-week "summer vacation" before it resumes its schedule on Aug. 24 with the Belgian Grand Prix at Spa- Francorchamps.