In the Braves’ first visit to Kauffman Stadium, Jason Heyward turned in a memorable performance for a sizable contingent of Braves fans who showed up for the event. Then Craig Kimbrel brought their hearts into their throats.

Heyward put the Braves ahead with a two-run double in the fifth inning, and put them ahead again with a seventh-inning homer that sent Atlanta to a 4-3 win Tuesday night against the Kansas City Royals. But not before a perilous ninth, when Kimbrel wiggled out of a two-on, no-outs jam for his 22nd save.

“Those are not the way you draw it up, but we’ll take it,” said manager Fredi Gonzalez, whose Braves took the opener of a two-game series and got their second consecutive win since losing three in a row and five of six.

Kimbrel walked the leadoff hitter in the ninth and gave up a single by David Lough to put Royals on the corners with none out in a one-run game. Then the formidable closer went to work, striking out Elliott Johnson and Jarrod Dyson, the latter after a stolen base put two runners in scoring position.

“I’d much rather it be 1-2-3,” Kimbrel said. “Whenever you walk the leadoff hitter in a one-run ballgame, it’s kind of a sticky situation.”

After falling behind 2-0 and electing to intentionally walk Alex Gordon to load the bases, he coaxed a game-ending fly ball by Alcides Escobar. Kimbrel’s expression conveyed more of an adrenaline rush than after a typical save.

“Of course,” he said. “I was that close to giving up the game, and then a few batters later we won the game. Any time that happens, I definitely get excited. Not for myself, but for the team.”

Heyward’s first three-RBI game this season helped Kris Medlen (5-7) improve to 4-1 with a 2.14 ERA in five June starts. Heyward was hit by a pitch in the back of his right elbow in the ninth and treated at first base, but stayed in and made the catch for the final out. He had some swelling afterward and wasn’t sure if he’d play Wednesday.

After the Braves got three runs on four consecutive hits to start the fifth inning, Medlen coughed up a 3-1 lead in the bottom of the inning on Eric Hosmer’s two-run, two-out homer.

But he recorded seven outs in the next seven batters, including a double-play grounder to end the sixth before he struck out all three batters in the seventh, after Heyward’s homer in the top of the inning gave the Braves another lead.

“You can’t give up two leads,” said Medlen, who allowed three runs, seven hits and one walk with five strikeouts in seven innings. “And for J-Hey to come up and hit that (home run) – I knew I had to have a nail-down inning.”

Braves reliever Jordan Walden worked around a leadoff walk in the eighth, as well as his errant pickoff throw and a stolen base. With a runner at second and one out, he struck out Billy Butler. With a man at third and two out, he coaxed a flyout from Salvador Perez, who ranked among American League leaders with a .364 average with runners in scoring position.

“Something (Kimbrel) hasn’t done in a while, walk the leadoff hitter,” Gonzalez said. “We did it in the eighth, also. In one-run ballgames, that’s tough duty. Both times we wiggled out of it. We throw a ball away in the eighth and Walden does a great job getting out of there. And we get first and third with (none) out in the ninth and a pretty good hitter up, and we got out of it.”

Royals starter Ervin Santana was 2-0 with an 0.94 ERA in four June starts before Tuesday, and was staked to a 1-0 lead in the first inning on Butler’s two-out single. Santana struck out four of the first seven batters he faced and didn’t give up a hit until Jordan Schafer’s two-out bunt single in the third inning.

The Braves wasted a scoring opportunity in the fourth inning after a leadoff, opposite-field double by Justin Upton, back after missing two games with a sore hand. Freddie Freeman followed him with a walk, but the next three Braves went down in order to strand the runners.

Given how Santana pitched recently — three earned runs in 32 2/3 June innings through Tuesday’s fourth inning — and Atlanta’s recent scoring woes, things didn’t look promising for the Braves at that point.

But then the skies unexpectedly opened and rained Braves hits in the fifth. Chris Johnson led off with a double, and Andrelton Simmons singled. Schafer followed with a tying run-scoring double, the sixth hit in eight at-bats for the Braves’ backup outfielder, who was 4-for-5 with a homer in Sunday’s win at Milwaukee.

Heyward followed with the fourth consecutive hit and third double of the inning, a two-run shot to the right-field corner for a 3-1 lead.

Hosmer, the hottest hitter on a Royals team that has struggled to score, homered in the fifth to give him 10 extra-base hit and 12 RBIs in his past 18 games, erasing the Braves’ lead.

The score was still 3-3 when Heyward homered with one out in the seventh off lefty Tim Collins, the sixth homer for Heyward and first allowed by Collins this season.

Heyward had only 13 RBIs before Tuesday and was 8-for-41 (.195) with one extra-base hit and one RBI in his past 11 games. But for the month of June, he’s up to .297 with four homers in 23 games.

“He’s quietly having a pretty good month of June,” Gonzalez said.

Collins threw three consecutive curveballs to Heyward, who looked at the first for a strike, swung and missed the next, then connected squarely with the third, a hanging pitch over the heart of the plate. He drive it over the fence in right-center for a 4-3 lead.

Like most watching, Heyward was surprised to see Collins throw another curveball after getting ahead in the count.

“I wasn’t looking for that one,” he said. “I wasn’t necessarily looking for it, but I was on time, didn’t miss that one, obviously, with two strikes. It was a big hit.”