The Raiders (2-11) followed their exhilarating first win of the season against Kansas City with a 52-0 loss at St. Louis before delivering their most complete performance of the season against San Francisco (7-6).
"It's great for us to come off of a bye week and perform the way we did today," said safety Charles Woodson, who sealed the win with a late interception.
"Being able to bounce back from the way we felt last week was big for us. To win this game, the Battle of the Bay, your crosstown rivals, it's a big win for us."
Carr completed 22 of 28 passes for 254 yards and completely outplayed counterpart Colin Kaepernick in his best performance yet in his rookie season.
With a second straight loss the 49ers (7-6) are in danger of missing the playoffs for the first time in Harbaugh's four-year tenure. San Francisco trails three teams by two games in the NFC wild-card race with three games remaining after making three straight NFC title games.
"There is no surrendering," Harbaugh said. "You look for the next thing to win at."
A Thanksgiving night loss at home to rival Seattle prompted CEO Jed York to apologize to fans on Twitter and raise even more speculation that Harbaugh will not be back next season for the final year of his five-year contract.
"We said last night in the meeting, that somebody had to step up and make a couple of plays," Sparano said. "Today I felt a bunch of guys stepped up."
"This week it was preached to us just how important this game was," Smith said. "This week more and more of us became true Raiders, knowing what the heritage is about, knowing what this game was about, how important it was to this city."
Carr rarely faced any pressure, giving him plenty of time to find open receivers downfield. He hit all five of his passes for 73 yards on the go-ahead drive capped by a 9-yard pass to Marcel Reece that made it 17-13.
"They were able to go out there and take advantage of the mistakes we made," linebacker Aldon Smith said. "They just made more good plays than we did. He did a good job of getting the ball out and using his protection."
Kaepernick threw an interception on the first play from scrimmage, was sacked on San Francisco's last and wasn't much better in between. He finished 18 for 33 for 174 yards with one touchdown, two interceptions and five sacks as San Francisco failed to score 20 points for the sixth time in seven games.
The Raiders went ahead 10-7 in the second quarter on a 3-yard touchdown pass to the 330-pound Penn, who bowled over Eric Reid at the goal line and leaped into the Black Hole for a beer-doused celebration. It was Penn's third career TD catch and second against the Niners, having also caught one in 2010.
"I'm happy I'm able to live every big man's dream," Penn said.
Copyright 2014 by The Associated Press