BUFFALO, N.Y. – The Detroit Red Wings got their legs going in the second period Saturday night and outskated and outplayed the Buffalo Sabres over the final 45 minutes of regulation and overtime at HSBC Arena.

Pavel Datsyuk and Jiri Hudler then scored in a shootout against Ryan Miller to lift the Red Wings to a 3-2 victory, extending their road winning streak to five games.

Detroit battled back from a 2-0 deficit during a sluggish first period, outshooting the Sabres 30-17 the rest of the way. Joey MacDonald, subbing for Jimmy Howard, made 30 saves in regulation and overtime and stopped Drew Stafford and Thomas Vanek in the shootout to even his record to 3-3-1.

“First period we were down a little bit, but we stuck with it,’’ MacDonald said. “We took over the game, started getting pucks to the net and putting a little more pressure on Millsie. He’s one of the best goalies in the league, he’s going to stop what he sees.’’

Hudler tied the game with 58 seconds to play in the third period. He hopped onto the ice as the extra skater after the Red Wings pulled MacDonald, and he went right to the net and banged in a rebound following Johan Franzen’s pass from the point that got deflected.

“I was heading straight to the net,’’ Hudler said. “Mule held onto the puck, there was a lot of people working in front.’’

Said Franzen: “It’s hard to score on the first shot on (Miller); he’s square and big, you got to get a rebound to get a goal on him. Bear (Danny Cleary) did a good job in front of the net, deflecting my pass, and Huds got the rebound.’’

Datsyuk and Hudler each scored on backhand shots in the shootout.

“Miller’s a good goalie, he’s fast side-to-side and a big goalie,’’ Datsyuk said. “I tried to fake it and shoot.’’

Said Hudler: “He’s a great goalie, it’s tough not to overthink before you go. I thought Pavel did a great job with the move on the first one, so I just went with his idea.’’

MacDonald made some big stops late in regulation, in overtime and the shootout.

“You never know what they’re going to do (in the shootout),’’ MacDonald said. “You just got to stay at the top of your crease, be nice and big and let them make the first decision.’’

Miller made 35 saves for the Sabres but didn’t get much support from his teammates after the first period.

“When you’re playing against Miller it’s a tough test early,’’ Red Wings coach Mike Babcock said. “I thought (MacDonald) was a bit nervous early, the puck came off him quite a bit, and then as the game went on he got better and our team got better as well.’’

The Red Wings welcomed back forwards Valtteri Filppula and Mike Modano to the lineup and their depth seemed to wear down the Sabres in the second and third periods.

Modano was back after missing 41 games following surgery to repair severed tendons in his right wrist. Filppula returned after sitting out eight games with a sprained left MCL.

“It was pretty good,’’ Modano said. “You get a little heavy as the game goes on, you try to not get caught out there too long. A couple times in our own zone you feel it pretty quick.’’

After a lousy start, the Red Wings skated much better and competed hard in the second period, outshooting the Sabres 12-7 and getting a power-play goal from Datsyuk to trim their deficit to 2-1.

The Sabres skated harder and seemed to have more in their tank in the first period despite having played the night before. The got goals from Andrej Sekera (12:55) and Mike Grier (14:59) during two-on-one situations.

The Red Wings took over from there, and now they continue this five-game road trip on the West Coast, starting Monday in Los Angeles.

“It’s huge, especially when you’re on the road for a week,’’ MacDonald said. “You got to start out with a win.’’

Franzen will return to Detroit, where his wife, Cecilia, is expecting their first child at any time. He will rejoin the team for Wednesday’s game in Anaheim.

“I was checking my phone between periods,’’ Franzen said. “I think I’ll make it to the delivery.’’