SAN DIEGO — Having hit his 400th career home run an inning earlier, Mark Teixeira couldn't resist a little bat flip after he hit No. 401.

Teixeira became the fifth switch-hitter with at least 400 home runs, Didi Gregorius also connected and Chad Green got his first big league victory as the New York Yankees beat the San Diego Padres 6-3 Sunday to avoid a three-game sweep.

Teixeira struck out in his first three at-bats before hitting a high, arcing shot into the home run porch down the right-field line off Carlos Villanueva leading off the eighth for No. 400. With Brett Gardner aboard on a single with one out in the ninth, Teixeira got No. 401 when he homered to right off Kevin Quackenbush.

"The 400, I didn't know if it was going out," Teixeira said. "I hit it so high, you never really know how the ball's going to carry."

No. 401 was a no-doubter, and thus the little bat flip.

"That one I could enjoy a little bit," he said.

Both balls were thrown onto the field by fans, so Teixeira got them and gave them to his father, who was at the game.

"Opposing teams love the Yankees," Teixeira cracked.

He joined Hall of Famers Mickey Mantle (536) and Eddie Murray (504) in the 400-homer switch-hitters club, along with Chipper Jones (468) and teammate Carlos Beltran (411) who reached the milestone this year.

"It's really cool. I feel connected to every one of the switch-hitters," Teixeira said. "Mickey Mantle played for the Yankees. Eddie Murray's the reason I'm a switch-hitter. I watched him growing up in Baltimore. I got to play with Chipper Jones for a year in Atlanta, which was an unbelievable experience, and now I'm playing here with 'Los and we do it in the same year. It's really cool. I'll always be able to take that with me."

He had not homered since April 13 before connecting against Minnesota last Sunday, and he homered against Texas on Monday. His first multihomer game since July 31 gave him seven home runs this season.

"Our reports obviously kind of failed us a little bit today because Teixeira got us on two breaking balls, a pitch he hasn't hit very well this year," Padres manager Andy Green said. "The one I thought Villa threw was a very good pitch."

The Padres were denied their first sweep of the season and what would have been their first sweep of the Yankees. New York beat San Diego 4-0 in the 1998 World Series.

Chad Green (1-1), a 25-year-old right-hander, held San Diego to one run and three hits in six innings, struck out eight and walked none. The rookie was recalled earlier Sunday from Triple-A Scranton/Wilkes-Barre, his third stint with the big league team this year.

It was a much better start than his big league debut, a 12-2 loss at Arizona on May 16 in which he lasted only four innings.

"From the first inning on, it just felt like another game," Green said. "I always thought I could get out big league hitters, but it's nice to have the reassurance. It's always nice to get the first one."

Green's only big mistake was Yangervis Solarte's leadoff homer in the second. Solarte tied his career high with four hits, finishing a triple shy of the cycle.

"I've definitely been battling through some things, but just trying to go out there every day and play hard and give it my best," Solarte said. "I didn't even realize I was a triple shy of the cycle. I was just trying to make good contact, hard contact, and go from there."

Aroldis Chapman relieved Anthony Swarzak after Alex Dickerson's two-run homer in the ninth, and got three outs for his 16th save in 17 chances.

Gregorius scored in the second when Ronald Torreyes hit into a double play and homered off Andrew Cashner (3-6) in the fourth. Aaron Hicks had three hits, raising his average to .208.

Cashner (3-6) came off the disabled list earlier in the day. Out since June 11 with a strained neck, he allowed two runs — one earned — and six hits in six innings.

Dickerson's homer was his second.

TRAINER'S ROOM

Yankees: Manager Joe Girardi said he's still trying to figure out how to use OF Carlos Beltran in the series at the Chicago White Sox that starts Monday. Beltran has a tight right hamstring.

Padres: Manager Andy Green said INFs Cory Spangenberg (left quadriceps strain) and Jemile Weeks (right hamstring strain) will head to the team's complex in Peoria, Arizona, to continue their rehabs.

UP NEXT

Yankees: LHP CC Sabathia (5-5) is scheduled to start Monday afternoon in the opener of a three-game series. Chicago will counter with James Shields (3-9), who was traded from San Diego to the White Sox on June 4.

Padres: Luis Perdomo (2-3) is scheduled to start the opener of a three-game series at Arizona starting Monday night. The Diamondbacks will counter with RHP Archie Bradley (3-3).