KANSAS CITY, Mo. — The following statement is either a source of true hope or genuine frustration, depending on your perspective: The Royals entered Sunday afternoon just three games out of first place and 2 back in the race for the second American League wild-card spot.

There is hope here, of course, because despite five straight losses, sandwiched around the All-Star break, the Royals remained in the thick of the postseason conversation.

There is frustration, though, because Royals manager Ned Yost spent much of the last week peering at the standings in the American League, where the Royals’ chief competition kept spinning their tires in the mud.

“The thing that makes it frustrating is that Cleveland and Minnesota are stumbling and bumbling around, too,” Yost said Sunday morning. “You have chances to pick up ground.”

More than six hours later, the exasperation would finally subside. The Royals walked off against reliever Jason Grilli for the second time this season, earning a 4-3 victory over the Texas Rangers at Kauffman Stadium.

Lorenzo Cain lofted a bases-loaded single that Texas right fielder Shin-Soo Choo lost in the sun. The ball bounced off his glove and was initially ruled a single. Alex Gordon trotted home from third base. The dugout emptied and swarmed Cain near first base.

The Royals had also beaten Grilli on June 23 at Kauffman Stadium, when he was a member of the Toronto Blue Jays. On that night, second baseman Whit Merrifield had a walk-off double during a four-run ninth.

The victory propelled the Royals (45-45) back to .500 and guaranteed they would not suffer a second consecutive sweep. They will return to Kauffman Stadium on Monday and open a four-game series against the Detroit Tigers.

The victory was not easy. Joakim Soria blew his sixth save opportunity — and second in the last week — after allowing a two-out double to Elvis Andrus and an RBI single to Nomar Mazara in the top of the eighth. Andrus clubbed a 2-2 fastball on the inner half of the plate. Mazara flicked an 0-1 fastball into center field, scoring Andrus from second base. The score was 3-3.

For a moment, it appeared as if the Royals would bust the streak after a breakthrough in the seventh. Merrifield roped a two-out double to left field against Texas starter Yu Darvish, scoring Drew Butera from first and providing a 3-2 lead.

The run was aided by an error in left field by Mazara, who bobbled the ball before losing his footing and unleashing a feeble throw toward the infield. The scoring decision rightfully robbed Merrifield of an RBI. Mazara would later atone for his sins. Yet, the wobble ensured that the slow-footed Butera would score without a play at the plate.

Royals starter Ian Kennedy allowed just two runs in 6 2/3 innings. The second run was a gut punch. On his 97th pitch of the afternoon, Kennedy served up a game-tying homer to Texas’ Mike Napoli with two outs. As Kennedy faced Napoli, reliever Peter Moylan was warming up in the bullpen.

The Royals built a 1-0 lead in the first inning on two singles and a wild pitch. But they would squander the opportunity for more, failing to take advantage of a throwing error by Darvish and two base runners in the second inning.

The Rangers evened the score at 1 on a solo shot from Drew Robinson in the top of the third. Eric Hosmer and Mike Moustakas responded with a one-two attack in the bottom half of the inning. Hosmer battled against Darvish, drawing a two-out walk. He scored from first when Moustakas roped a double into the right-center gap.