The Marlins may never tire of extra-inning games as long as they keep winning them.

Playing their fourth extra-inning game in the past 10 days, the Marlins handed the Brewers a 5-4 loss in 12 innings on Starlin Castro’s walk-off single.

“I’m the hero and I won the game,” Castro said, clearly joking.

With Wednesday’s outcome, the Marlins improved to 5-4 on the season in extra-inning games. But they’ve developed a knack for winning them of late. They defeated the Rays in 10 innings on July 2, knocked off the Brewers in 10 on Monday, and won another one in overtime on Wednesday in front of the smallest crowd in Marlins Park history: 5,265.

They lost a 16-inning contest to the Rays on July 3.

“Getting used to these,” said manager Don Mattingly.

The Marlins are no longer the pushovers they were at the start of the season. The Brewers own the best record in the National League and had swept the Marlins in four games in Milwaukee back in April.

But the Marlins are starting to come around even though they and the Mets share the worst winning percentage in the N.L. They’ve gone 19-17 since June 4 and have won seven of their past 11 series.

Dan Straily, who started Wednesday for the Marlins and turned in six strong innings, said he’s noticed the team’s younger players gaining confidence with more experience.

“We’re getting better and better every single day I feel like,” Straily said. “You start to see guys relaxing and kind of become themselves as players. Younger guys start to realize that, man, I really can play here. I really do belong here. And that’s when you start to see more wins come along.”

Straily struggled in the early going Wednesday, giving up a run in the first and another in the third as the Brewers took a 2-0 lead.

But the Marlins struck for three runs in the fourth after Brian Anderson delivered the Marlins’ first hit, a triple off Freddy Peralta, and scoring on Castro’s sacrifice fly. Justin Bour tied it with his 15th home run and Straily aided his own cause when he drew a bases-loaded walk.

After the Marlins made it 4-2 in the sixth on Bryan Holaday’s RBI single, the Brewers tied it in the eighth on Jesus Aguilar’s two-run double off Drew Steckenrider.

But the Marlins weren’t about Aguilar hurt them again when he went to the plate with a man aboard in the 12th and Rule 5 pick Elieser Hernandez on the mound. The Rockies sent Erik Kratz to the on-deck circle to pinch-hit for Jorge Lopez. But Mattingly sensed it was a decoy and had Hernandez pitch around Aguilar for a walk.

“You never know,” Mattingly said. “But we felt like when they brought (Lopez) in, he was in for the game. We just felt like that guy was going to have to hit.”

Related stories from Miami Herald

Mattingly nailed it. Kratz returned to the dugout, Lopez went up to hit, and Hernandez struck him out to end the inning.

After Garrett Cooper singled to start the 12th for the Marlins and advanced to second on JT Riddle’s ground ball, the Rockies had Anderson walked intentionally to set up the force.

But Castro shot a single through the left side of the infield and third base coach Fredi Gonzalez, knowing left fielder Christian Yelich doesn’t possess an outstanding arm from his days in Miami, waved Cooper to the plate.

“We know Yeli,” Mattingly said. “He’s obviously a really good player. But probably the one downfall of him, he doesn’t throw great. He has a decent arm, but he doesn’t get rid of it quick. You’ve got to challenge there. You’re trying to win a game. Make him throw you out.”

Yelich’s throw was way off target, allowing Cooper to score the winning throw without a slide.

Hernandez pocketed his first major league win.