FOXBOROUGH, Mass. — Gillette Stadium was just one big backyard for Tom Brady and the New England Patriots on Saturday.

Faced with a two-touchdown deficit and some boos from an impatient crowd, Brady and his offense started improvising.

Formations that used only four offensive linemen. A pass from one wide receiver to another.

The Baltimore Ravens couldn’t keep pace, losing their lead and eventually the AFC divisional playoff game, 35-31. New England became the first team in NFL playoff history to overcome a pair of 14-point deficits and pull out a victory. The reward is a fourth consecutive berth in the AFC championship game, which will be back in the Patriots’ backyard next Sunday against either the Indianapolis Colts or the Denver Broncos.

“It was so fun,” Patriots offensive tackle Nate Solder said. “We were lucky enough to pull off some awesome plays when we needed them.”

New England overcame an early 14-0 deficit, only to fall behind 28-14 early in the third quarter. That’s when the Patriots turned to deception, lining up with only four offensive linemen while having either running back Shane Vereen or tight end Michael Hoomanawanui split wide, but ineligible as a receiver.

Brady completed six consecutive passes while the Ravens looked befuddled and Coach John Harbaugh fumed on the sideline, eventually picking up an unsportsmanlike conduct penalty while protesting the Patriots’ ploy. Eventually, Brady hit Rob Gronkowski for a 5-yard touchdown to pull New England within 28-21.

“That was something that the coaches told us that they wanted to do. We’d never done it before,” Solder said of the unique alignment. “They come up with all kinds of stuff, so any opponent has to be ready.”

Said Ravens defensive end Chris Canty: “They threw a lot of different formations at us, guys eligible, guys ineligible. They did a lot of things to keep us on our heels.”

The Ravens had to punt on their next possession, and Brady then connected on two passes for 19 yards before the Patriots unveiled a “double pass” play that they’d been working on for years but had never attempted in a game.

Brady threw a backward pass to wide receiver Julian Edelman near the left sideline. Edelman, who played quarterback at Kent State, then flung a pass to a wide-open Danny Amendola for a 51-yard touchdown that tied the score and unleashed bedlam among the 68,756 fans.

“We dialed it up a few times in practice against the coverage they were giving us. We kept on seeing that coverage and we were able to get it called,” Edelman said after becoming the first non-quarterback to throw a touchdown pass for New England since kicker Adam Vinatieri in 2004. “It was a fun play and I’m happy it got executed, because that means I’ve got a light to maybe throw again.”

Brady ended up with 367 yards and three touchdown passes on a day when New England managed only 14 yards on the ground. But he was envious of the throw his receiver made.

“He spun it. It was a perfect spiral, right in stride,” Brady said. “I’ve got to make some rules they can’t throw it better than I can.”

Amendola caught five passes for 81 yards and two touchdowns in his best game as a Patriot. His 15-yard touchdown in the second quarter, in which he took a short pass, faked out safety Matt Elam and dived inside the left pylon, tied the score 14-14.

“Playing meaningful football at the end of the season is what we all dream about, is what we all work hard for,” Amendola said. “We’ve got a lot of great football players on this team, and we’ve just got to get them in position to make plays.”

In the fourth quarter, Baltimore finally foundered in the red zone, settling for a 25-yard field goal by Justin Tucker for a 31-28 lead.

You don’t bring a field goal to a touchdown fight.

Brady had his opening, and he seized it, moving New England 74 yards in 10 plays and capping it with a beautifully thrown fade route to Brandon LaFell for a 23-yard score that finally put his team ahead.

“(The defender) looked like he had his right arm kind of arm-barred and he caught it with his left and he was able to get his right hand on it,” Brady said of LaFell. “It was a great play. We needed it.”

The victory wasn’t sealed until Patriots backup safety Duron Harmon intercepted a Joe Flacco pass in the end zone with 1:39 left. But it was orchestrated by Brady, a postseason winner for an NFL-record 19th time.

“You don’t want to say you expect that, but I mean, it’s Tom Brady,” Edelman said. “It’s always nice to have that guy.”