MIAMI GARDENS, Fla. -- The Buffalo Bills ended their 18-year playoff drought after a 22-16 win Sunday over the Miami Dolphins and the Baltimore Ravens' last-second 31-27 loss to the Cincinnati Bengals.

In a scramble to the finish, the Bills took the final postseason berth when Cincinnati stunned Baltimore, scoring on a fourth-and-12 play when Bengals quarterback Andy Dalton threw a 49-yard touchdown pass to Tyler Boyd with 44 seconds remaining.

Buffalo (9-7) earns the No. 6 seed and will travel to play the Jacksonville Jaguars. The wild-card game (Sunday, 1 p.m. ET, CBS) will be Jaguars coach Doug Marrone's first against Buffalo as a head coach since resigning as Bills coach on Dec. 31, 2014.

"It's such an emotional moment," first-year Bills coach Sean McDermott said in the locker room after the Ravens' dramatic loss. "No one gave this team a chance all year long. Our fans have been great all season long, and I'm looking forward to seeing them at our playoff game."

After beating the Dolphins, some Bills players chanted "Who Dey!" in support of the Bengals, who were trailing 27-24 to the Ravens when the Bills entered the locker room. McDermott said players were praying and holding hands, and that some cried when the Bengals stunned the Ravens.

The Bills tweeted their thanks to the Bengals on Sunday night.

The Bengals also tweeted a shoutout to Bills fans who suddenly reached out on social media.

For all the @buffalobills fans in our mentions:



Happy New Year 🎉 #Bengals50 — Cincinnati Bengals (@Bengals) January 1, 2018

The Bills still face long odds. Their odds to win the Super Bowl when the season began were 200-1. After Week 8, when they were a surprising 5-2, their odds were 50-1. Three weeks later, after three consecutive losses, they were 500-1. And after Sunday's game, 100-1.

On the other hand, the Ravens' projected chances of reaching the postseason were at 97 percent before their game with the Bengals began, according to ESPN's Football Power Index. Among the other teams that had not clinched a spot yet, none had more than a 68 percent chance.

"It's tough," Ravens quarterback Joe Flacco said after the game. "Those are guys in there [the locker room], we've gone to battle on the football field together for 17 weeks, and you try to create something special and get yourself in the playoffs. So when you put that kind of work in and [you] aren't quite good enough, it's a tough pill to swallow."

Ravens linebacker Terrell Suggs said the team was stunned. "We went from having all our dreams come true to having it go away in a matter of seconds," he said. "It's like a bad dream. You can't believe this happened to us."

The Bills qualified for the playoffs for the first time since 1999, when they were eliminated by the Tennessee Titans on a last-second kickoff return for a touchdown that became known as the "Music City Miracle."

"It's very similar to having the anvil that we had for my whole 20-year career about the team staying in Buffalo -- that anvil, I always said we threw into Lake Erie when [owners] Terry and Kim [Pegula] bought the team, and now we have another anvil we can throw in Lake Erie," team president Russ Brandon, who has been with the Bills since 1997, said after the game. "It's about the fans and about the people who have supported us through thick and thin, that they don't have see that graphic on ESPN anymore about the drought.

"It's just something that we shouldn't deal with. The community is in such a great place. Everything that has happened in our community. It's just nice to see everything happen for Terry, Kim, Sean, [general manager] Brandon [Beane] and everybody else that has done a great job."

Buffalo's streak of 17 seasons out of the playoffs was the longest active drought in the NFL and the longest in NFL history that took place entirely after the 1970 AFL-NFL merger.