EAST RUTHERFORD, N.J. -- Rex Ryan might see yellow penalty flags in his sleep.
Luckily for him and his New York Jets, an ugly afternoon still ended with an improbable victory.
Geno Smith threw two touchdown passes, including a go-ahead 69-yarder to Santonio Holmes in the fourth quarter, and the Jets overcame a team-record 20 penalties to hang on and beat the Buffalo Bills 27-20 Sunday.
"I love the fact that our team found a way to win," Ryan said. "We persevered and we won. That's really encouraging to me. That tells you about the fight and spirit of this football team, and that's what I'm most proud of."
Smith, who also ran for a score, slightly outplayed EJ Manuel in a matchup of the first two quarterbacks selected in the NFL draft.
But this one was tough to watch with the barrage of penalties. The Jets (2-1) rolled up 168 yards in penalty yardage, and nearly gave it away against the Bills (1-2).
New York became the first team to get called for that many penalties and win since the Browns beat the Bears in 1951 despite 21 penalties.
"I know this team is going to get better," Ryan said. "There's no way we can't. Twenty penalties? That's on my shoulders. No question about it, but I know we can get better."
Capping a drive kept alive by four straight penalties on the Jets, Manuel connected with Scott Chandler for a 33-yard touchdown, then hit Stevie Johnson with a pass for 2-point conversion that tied it at 20 with 10:39 left in the game.
"We had a huge momentum swing there and couldn't capitalize on it," Johnson said. "We made our mistakes, but they made 20 penalties and we didn't win. We have to find a way to get it done with that kind of help."
Smith got the Jets right back onto the scoreboard with his big heave to Holmes, who broke away from Justin Rogers as he caught the ball and zipped into the end zone.
"We knew that we had to score," Smith said.
Ryan lost two challenges in a span of three plays in the third quarter and it ended up hurting the Jets.
Manuel scrambled for 21 yards early in the fourth quarter, but lost the ball when he was hit by Dawan Landry and Jaiquawn Jarrett recovered. But officials ruled Manuel was down -- and Ryan had no more challenges, with replays showing Manuel appeared to fumble.
New York then had a horrendous sequence of four straight penalties, three on Kyle Wilson, who was benched briefly after the last of the calls.
That kept Buffalo's offense on the field. It appeared Fred Jackson also had a fumble during the drive that the Jets recovered, but penalties also negated that and put the Bills in a position to tie.
"We were able to get the game to 20-20," Bills coach Doug Marrone said, "but then we were not able to make the plays to get us over the hump."
Smith, the second quarterback drafted at No. 39, finished 16 of 29 for 331 yards and also had two interceptions.
Manuel, who went No. 16 overall as the first quarterback drafted, was 19 of 42 for 243 yards and the score.
It was a costly defeat for Buffalo, which lost cornerback Leodis McKelvin to a hamstring injury, defensive tackle Marcell Dareus to an ankle injury and defensive end Alex Carrington was carted off late in the game with an apparent left knee injury.
Running back C.J. Spiller also left with a knee injury and finished with just 9 yards rushing.
Smith was sharp on the Jets' first possession, marching New York 80 yards and converting three third downs before punching it in himself from 8 yards on a quarterback draw to make it 7-0. Dan Carpenter's 37-yard field goal made it 7-3 in the second quarter.
Buffalo got the ball right back when Smith lofted a pass for Holmes into double coverage and was picked off by former Jets safety Jim Leonhard, and Carpenter made a 23-yarder moments later to cut it to 7-6.
The Jets took a 14-6 lead on a pretty pass from Smith, who hit Hill in stride for a 51-yard touchdown. Nick Folk kicked a 47-yard field goal into the wind to make it 17-6 as time expired in the opening half.
"How many games are we going to win with two turnovers and 20 penalties? Not many," Ryan said. "We will get it corrected. I will get it corrected."
After Buffalo allowed just one sack in its first two games, New York had eight, including two by Muhammad Wilkerson. ... Jets DB Ellis Lankster saved Ryan Quigley's 40-yard punt from going into the end zone, and Isaiah Trufant downed it at the 1 with 21 seconds left in the game. ... Jets WR Clyde Gates had his right leg wrapped and he limped as he left the stadium. RB Chris Ivory left the game with a hamstring injury.