Entering its final midweek tuneup before its last Big Ten series of the regular season, Maryland baseball needed any semblance of a spark to buck its recent skid of five losses in six games.

As the Terps came up to bat in the seventh inning down 4-2 against James Madison on Tuesday, it looked as if another rocky result was in store for the struggling squad.

But when center fielder Chris Alleyne reached on a walk to lead off the frame, he provided the much-needed jolt to help bring his team back.

Alleyne stole second and third before scoring on an AJ Lee single, cutting the Dukes’ lead down to 4-3. Red-hot right fielder Randy Bednar — who extended his hitting streak to 10 games in the fourth inning — then stepped up to the plate as the Terps’ leader in batting average, hits and RBI.

The sophomore added to each of those totals with a green-light swing on a 3-0 count, hitting a two-run homer off right-hander Grayson Jones to give Maryland the lead — one it wouldn’t relinquish as coach Rob Vaughn’s squad found late momentum to defeat James Madison, 6-5.

After dropping two of their three matchups against conference foe Minnesota, the Terps hoped to put up a good performance as they prepare for their weekend series against Iowa. Entering the day, Maryland sat half a game outside the No. 8 seed — a seed that would earn the Terps qualification for the Big Ten tournament.

Left-hander Drew Wilden earned the start for the Terps, but only lasted two innings. James Madison got off to a quick start as third baseman Trevon Dabney led off with a double in the first frame. Dabney then advanced to third on a passed ball before scoring off a groundout from shortstop Fox Semones.

The struggles continued for the Terps in the bottom of the second inning, as Dukes second baseman Matt Dipasupil laced a single to left field before stealing second. Freshman catcher Zach Doss — making his first college start — suffered his second passed ball of the game as Dipasupil took third before catcher Travis Reifsnider brought him home with an RBI single, pushing James Madison’s lead to 2-0.

The Terps eventually settled on defense, with pitchers Elliot Zoellner and Mark DiLuia combining to toss three hitless innings. However, Maryland’s offense remained stagnant, mustering only two hits in the first four innings.

In the fifth inning, Vaughn’s lineup started to show signs of life, with Alleyne and second baseman Benjamin Cowles posting back-to-back two-out singles. However, Doss was called out on strikes to end the inning and end any hint of a Terps’ threat.

Maryland rallied in the top of the sixth, though, loading the bases with no outs. However, the Terps’ hopes looked dashed when first baseman Maxwell Costes grounded into a double play that got the lead runner. But left fielder Michael Pineiro stepped up with a two-RBI double, tying the game at two.

James Madison surged back, as left-hander Sean Fisher gave up two runs in the bottom half to put the Dukes up 4-2. The Terps responded with a three-run inning, as Lee brought Alleyne home with an RBI single before Bednar’s blast gave Maryland the lead for good.

Lee added to the Terps’ lead in the ninth, hitting his fourth homer of the season, to extend Maryland’s lead to 6-4. While the Dukes added a run in the bottom of the ninth to add some pressure, right-hander John Murphy induced a ground out to end the game, pushing Maryland to 25-27 on the year.

The Terps will hope that gutsy nature of their victory will push the team to an impressive showing this weekend, as the Terps return to Bob “Turtle” Smith Stadium to take on Iowa in a three-game series that has considerable Big Ten tournament implications.