EAST LANSING, Mich. (AP) On a rare day when Michigan State's defense was struggling, Brian Lewerke and Darrell Stewart helped the Spartans prevail.

Lewerke threw two touchdown passes to Stewart, and those two connected again for a big gain on Michigan State's final drive, which ended when Matt Coghlin made a tie-breaking 21-yard field goal with 5 seconds remaining. The 25th-ranked Spartans beat Indiana 40-31 on Saturday - after Coghlin's kick gave Michigan State a 34-31 lead, the Hoosiers tried to lateral their way down the field on their last offensive play, but that resulted in a fumble the Spartans recovered for a touchdown.

''It definitely feels good to be able to score when we need to and drive down there at the end and get points, when the defense wasn't having their best day,'' Lewerke said. ''Which doesn't happen a lot.''

Lewerke threw for 300 yards and three touchdowns in a game that turned into quite a quarterback duel. Michael Penix Jr. returned for Indiana (3-2, 0-2 Big Ten) after missing the previous two games with an undisclosed injury - and he gave the Spartans (4-1, 2-0) fits, completing 20 passes in a row during one stretch.

Penix threw for three touchdowns, including an 11-yarder to Whop Philyor that tied the game at 31 with 2:00 remaining. Lewerke immediately led the Spartans back down the field. Stewart outfought defensive back Jaylin Williams for a 44-yard reception, then Lewerke broke free for a 30-yard run that put the ball at the 1.

Rather than try to score right away, the Spartans ran the clock down and set up Coghlin's kick.

''Tough way to lose. Proud of our team, how our kids fought,'' Indiana coach Tom Allen said. ''It's hard to face them in the locker room knowing how hard they played. Look them in the eyes, and there's a lot of tears in there. And there should be.''

Elijah Collins gave Michigan State a 7-0 lead with a 4-yard scoring run, but Indiana answered late in the first quarter with a 28-yard TD pass from Penix to Philyor.

Lewerke put the Spartans back ahead with a 5-yard touchdown pass to Stewart in the second, but Penix scored on a 2-yard run to tie it at 14.

Lewerke found Stewart for a 26-yard TD with 33 seconds left in the half.

Penix completed his last seven pass attempts of the first half and his first 13 of the second, but Michigan State held the Hoosiers to a field goal in the red zone to start the third quarter.

Indiana went ahead 24-21 with 14:52 remaining in the fourth on a 12-yard touchdown pass from Penix to Donavan Hale, who appeared to make the catch with one hand while cornerback Josiah Scott was draped all over him.

Michigan State went back ahead 28-24 with 12:12 remaining when Lewerke threw a 10-yard scoring pass to Matt Seybert.

THE TAKEAWAY

Indiana: The Hoosiers had an impressive game plan, throwing quick, short passes and largely negating Michigan State's pass rush. It wasn't enough for a win, but Penix is a redshirt freshman and seems to have a bright future.

''The coaches put us in the right situation, play calls, the receivers getting open,'' Penix said. ''Just catch and throw. That's all we always preach - catch and throw. We're out there just playing our hearts out, just protecting each other and making sure we're on the same page.''

Michigan State: The Spartans' offense has been deservedly criticized this year, but this is now back-to-back games with more than 30 points. Lewerke and Co. responded under pressure late in this one.

CRUCIAL FLAGS

The teams combined for 15 penalties, including a few huge ones. Penix's second-quarter touchdown run was set up by a pass interference penalty in the end zone. In the fourth, the Hoosiers had a long punt return negated by a penalty that brought the ball back to the Indiana 11. That flip in field position ultimately helped Michigan State take over the ball in Indiana territory before the drive that put the Spartans up 28-24.

When the Hoosiers were driving to tie it at 31, they were aided by two pass interference penalties and a roughing the passer call.

GAME MANAGEMENT

Lewerke said he wasn't sure whether he should score on his long run toward the end - or if the Spartans should try to run more time off the clock.

''I was aware of all that stuff, so I was kind of thinking about whether or not I should score or not,'' Lewerke said. ''Ended up working out well.''

Coach Mark Dantonio sounded a little skeptical of that explanation - but was happy to be able to run the clock down.

''I thought he was tackled, but we'll go with what he's saying - he's hot,'' Dantonio said. ''I think that it was the right thing to do, and then we just wanted to kick the field goal at the end of the game. We didn't want to score and allow them any time on the clock.''

NOTABLE

Philyor had 14 catches for 142 yards. ... The Spartans allowed 30 points for the first time in 17 games. ... Dantonio was honored after the game for taking over the school's career lead in victories the previous weekend at Northwestern. ... Dantonio said LB Brandon Bouyer-Randle has entered the transfer portal. He also said RB Connor Heyward missed the game for personal reasons. WR Weston Bridges was out because of what Dantonio called a minor team rules issue.

POLL IMPLICATIONS

The Spartans were barely in the Top 25, and this wasn't an especially convincing win - but unlike in their previous home game against Arizona State, they were able to win in the end and retain the Old Brass Spittoon.

UP NEXT

Indiana: The Hoosiers are off next weekend before hosting Rutgers on Oct. 12.

Michigan State: The Spartans play at Ohio State next Saturday night.

---

Follow Noah Trister at https://twitter.com/noahtrister

---

More AP college football: https://apnews.com/Collegefootball and https://twitter.com/AP-Top25

Copyright 2019 by STATS LLC and Associated Press. Any commercial use or distribution without the express written consent of STATS LLC and Associated Press is strictly prohibited.