SAN FRANCISCO — Rafael Devers muscled up and got nothing in the 10th inning.

The Los Angeles Dodgers failed to hit the ball hard against Spencer Bivins in the 11th yet broke the game open.

“Baseball can be a cruel game,” Giants manager Bob Melvin said. “It’s just the way baseball is sometimes. Unfortunately, it didn’t go our way.”

The Giants enter the All-Star break pondering the wicked nature of their sport after a 5-2, 11-inning loss to the Dodgers on Sunday at Oracle Park.

The Giants (52-45) lost two of three to the National League West-leading Dodgers (58-39) in the series and will be six games back of their longtime rivals when play resumes on Friday.

When it looked as if the Giants would go quietly, pinch-hitter Luis Matos tied the score with a two-run home run to left center in the ninth.

Joey Lucchesi pitched a scoreless 10th for the Giants, with free runner Andy Pages getting no further than third base.

In the 11th, the Dodgers scored three times -- a bloop run-scoring single by Freddie Freeman, an infield single on a chopper by Teoscar Hernandez which drove in a run, and a bloop RBI single to right by Pages. That made Bivins (2-3) a hard-luck loser.

With two out in the 11th, Freeman’s ball left the bat at 70.5 mph and found open space 242 feet away. The dribbler by Hernandez traveled 4 feet, with Wilmer Flores and Bivins unable to make a play. Pages’ hit traveled 173 feet at 64.3 mph.

Did Bivins feel any better because he had essentially done his job?

“Honestly, right now? No,” Bivins said. “But it’s nice to have support from your team. We’ve got some tough (guys) in this clubhouse.”

In the bottom of the 10th, Devers looked as if he’d sent the Giants home happy with a line drive to deep center with one out and runners at first and second. It left the bat at 106.1 mph and carried 381 feet, but center fielder Pages managed to track it down against the fence for the second out. Willy Adames grounded to third against Ben Casparius for the third out.

Down 2-0 in the ninth, the Giants electrified the home crowd when Matos, pinch-hitting for Mike Yastrzemski, hit a one-out, two-run home run off a slider to left center against Dodgers closer Tanner Scott to tie the score 2-2.

“It was very exciting, especially with the atmosphere at the ballpark, coming in in that situation and being able to contribute to the team,” Matos said through interpreter Erwin Higueros. “Unfortunately, we didn’t get the win, but it felt good.”

Matos’ homer, his fifth of the season, drove in Matt Chapman, who had a one-out single as the Giants went into the ninth with just three hits.

The chaotic ending came after All-Star pitchers Yoshinobu Yamamoto and Robbie Ray looked the part through most of the game. Yamamoto threw 91 pitches, 64 of them strikes. Ray was done in favor of Ryan Walker after six innings, giving up two runs on three hits with two walks and six strikeouts. He threw 92 pitches, 54 of them strikes.

“I felt like I did a pretty good job of mixing it up and kept them off balance for the most part,” Ray said.

Ray, who was moved up in the rotation and won’t pitch in the All-Star game as a result, said it was a no-brainer to pitch against the Dodgers.

“Getting recognized and making the team is great, and I’m super thankful for that,” Ray said. “But we’re getting serious here, and when they asked me to pitch this game, it made the most sense.”

Miguel Rojas homered for the Dodgers in the fifth inning, his fifth of the season.

After retiring the first nine men in order on just 24 pitches (17 strikes), Ray walked Shohei Ohtani on a 3-2 pitch to lead off the fourth and then walked Mookie Betts on a 3-1 pitch.

Will Smith flied to shallow left, and Freeman followed with a first-pitch double to right, scoring Ohtani and sending Betts to third. Ray averted further trouble in a 26-pitch inning by striking out Hernandez swinging and Pages looking.

Ray had to work even harder in the fifth, which included a solo home run by Rojas to left field with one out. It took Ray 30 pitches to get out of the inning, which also included a single by Ohtani.

Rojas’ home run came on a full count and left the bat at 102.7 mph and carried 383 feet, well behind Heliot Ramos’ attempt to climb the fence for a miracle catch.

Devers was 0-for-5 with two comebackers and a strikeout against Yamamoto and a strikeout against Vesia in the eighth before his lineout in the 11th. He is hitting .202 (18 for 89) in 25 games since joining the Giants by trade from the Boston Red Sox on the final day of the Giants’ series at Dodger Stadium nearly a month ago.

Besides adjusting to a new team, Devers has also been playing with groin and back issues.

“It will be a nice break for everybody, including him,” Melvin said. “That last one was a good one. Just got unlucky there.”

Walker, Randy Rodriguez, and Camilo Doval each pitched a scoreless inning in relief for the Giants. Casparius (7-3) was the winning pitcher for the Dodgers.

WEBB PONDERING

With Ray already out of the picture in terms of All-Star availability, Melvin said the status of starter Logan Webb is up in the air “depending on how he feels.”

Webb sounded game when talking to reporters afterward, saying if he did pitch, it would probably be early.

Webb (9-6) started Friday night’s 8-7 win over the Dodgers, pitching 5 1/3 innings and throwing 91 pitches to get the win.

Melvin said he wouldn’t set the post-All-Star break rotation “until we know what he’s doing.”

Hayden Birdsong, who was skipped in favor of Ray to finish out the All-Star break, will go back into the rotation when the Giants resume play Friday in Toronto.

“It’s been a good little break for him,” Melvin said. “He’s available in the bullpen.”

Melvin said he hadn’t talked with Dodgers manager Dave Roberts, who is leading the National League team, about the usage of Webb and Rodriguez.

“I hope they use Randy for about five pitches and get him out,” Melvin said.