PASADENA — Slovakia’s Peter Sagan surged late and won the Tour of California by 3 seconds Sunday with a close third-place finish in the final stage.

Britain’s Mark Cavendish of Etixx-Quick Step raced to his fourth stage victory in the eight-leg event in a mass sprint in the last of nine circuits around the Rose Bowl.

Sagan, the Tinkoff-Saxo rider who began the final day trailing France’s Julian Alaphilippe of Etixx-Quick Step by 2 seconds, gained 4 bonus seconds for the third-place finish and a 1-second bonus during a mid-race sprint.

Dutchman Wouter Wippert of Drapac was second in the final stage. Sagan edged American Tyler Farrar of MTN-Qhubeka by a few inches for third to secure the title.

Colombia’s Sergio Luis Henao Montoya of Sky finished third overall, 37 seconds behind Sagan.

“I almost thought that I didn’t do it,” Sagan said. “(Tyler) Farrar was third, I thought, but after my team said that I was third, and I came here (to the podium) and I can’t believe that I did it.”

Sagan, who won two stages in the event, held the race lead after winning the Stage 5 time trial. He collapsed at the finish line after finishing sixth in the mountainous Stage 7 to Mount Baldy and relinquished his lead.

“It was very hard to get this victory,” Sagan said. “I did surprise myself from the effort yesterday and I believed that I could do it today. I never thought about going for the general classification at the Tour of California. This year it was very hard to finish on the climb.”

Cavendish completed the 65.3-mile race from Los Angeles to Pasadena in 2 hours, 14 minutes, 55 seconds.

“We tried our best, but Tinkoff rode very well, especially Peter,” Cavendish said. “He chased down breaks by himself. It was impressive.”

The eight-day race began May 10 in Sacramento with a field of 143 riders from 18 teams. There were 121 finishers.