Kwiatkowski, the 2014 world champion, claimed his first Milan-San Remo after launching his sprint late to stun Sagan at the finish in San Remo.
Julian Alaphilippe (Quick-Step Floors) finished third to claim a podium place on his debut after 291km of racing from Milan.
"I'm very happy, although I actually didn't expect to win," Kwiatkowski said, "I won Strade Bianche recently, and now to come and win 'La Primavera' is just incredible."
Sagan started Milan-San Remo as the man to beat from a high-quality field that included 2014 champion Alexander Kristoff (Katusha), 2015 winner John Degenkolb (Trek-Segafredo) and Arnaud Demare (FDJ), who won in 2016.
Kristoff went on to finish fourth at five seconds in arrears and at the head of a chasing bunch featuring Fernando Gaviria (Quick-Step Floors), Demare, Degenkolb, Nacer Bouhanni (Cofidis) and Viviani.
As expected, Sagan made the decisive attack on the Poggio six kilometres from the finish, with Kwiatkowski and Alaphilippe the only two riders with the legs to respond.
After establishing a gap on the chasers, Sagan led into the final kilometre before launching his sprint nearly 350 metres from the finish.
The race then came down to a drag race between the trio with Kwiatkowski jostling past to snatch the victory at the line.
"He (Sagan) actually made the race," Kwiatkowski said.”But when he escaped, I knew we absolutely had to catch him."
It was Sagan's second runner-up place after his second behind the now retired Gerald Ciolek in 2013.
"I've got used to second here, though I was expecting something different," Sagan said. "The final went as it did. Both of them took turns with me, but I thought I had the legs to go for a long sprint.
"The results don't matter. It's important to give the fans a bit of a show."
TOP TEN FOR EWAN
Caleb Ewan (C) confirmed his class after finishing 10th at Milan-San Remo. Source: Getty
The best placed Australian was Orica-Scott's Caleb Ewan who finished 10 in his first Milan-San Remo. Michael Matthews (Sunweb) finished 12th and Simon Gerrans 36th. All were given the same time of five seconds behind the winner.
“It went pretty quickly actually,” Ewan said. “My legs feel alright. I think because the first part was easier it didn’t tire me out too much, I guess it just weakens you a little bit.”
“I thought if I was on a good day and in good position on the climbs that I could be there in the finish. I felt good in training in the last week but I knew it would depend on how it was raced and maybe today was a bit easier than other years.
“Today gives me confidence that it can happen one day.
“Going into the race having not done it before you never really know how you will get over the climbs but I felt pretty comfortable so it gives me confidence for the future."
(L-R) Peter Sagan, Michał Kwiatkowski and Julian Alaphilippe. Source: Supplied