It is only the third time in major league history that two perfect games have been thrown in the same year. There were 130 years between the first and second such occasions and just two years between the second and third.
Cain, 27, won his seventh straight start while striking out 14. He improved to 8-2 for the year and his earned run average dipped to 2.17. It was the sixth shutout and 15th complete game of his eight-year career.
It was also the first perfect game in the 130 years of the Giants franchise. His perfect game was the 22nd in major league history and the 277th no-hitter.
The other perfect game in 2012 was thrown by Philip Humber of the Chicago White Sox on April 21. Since then there have been no-hitters by Jered Weaver of the Los Angeles Angels, Johan Santana of the New York Mets and a combined effort put together by a record-equaling six members of the Seattle Mariners.
Lee Richmond and John Montgomery Ward were credited with throwing perfect games in 1880 and it was not until 2010 that two perfect performances were again turned in during the same season. Dallas Braden of Oakland and Roy Halladay of Philadelphia delivered perfect games 20 days apart in May of that year.
San Francisco right fielder Gregor Blanco kept the masterpiece alive by making a spectacular, diving catch of a ball hit by Jordon Schafer in the seventh.
The final out of the game was made by pinch-hitter Jason Castro, who hit a hot grounder to third base. Joaquin Arias moved to his right to make what was not a routine play, but fielded the ball cleanly and got Castro by a stride at first to touch off the San Francisco celebration.
Melky Cabrera, Brandon Belt and Blanco all hit two-run homers as part of the Giants' 15-hit attack. Eight of the runs were given up by J.A. Happ (4-7) in 3 1/3 innings.
Jordana Brewster on Paul Walker: 'He was an enormous presence in my life'
Beyonce flaunts bikini body, Blue Ivy in vacation pics