SAN FRANCISCO — The 49ers asserted themselves on both sides of the ball against the Dolphins on Sunday. And yet they had a hard time dispensing of the visitors from Miami. The Dolphins hung around long enough to cut the score to 20-13 about halfway through the fourth quarter, and got the ball soon after that with a chance to drive to a tie.

It was probably a little more dramatic than the Candlestick Park crowd had anticipated. But the tension finally disappeared in the time it took Colin Kaepernick to run a 50-yard dash.

With 2:10 left, Kaepernick put the ball in Frank Gore's midsection on a zone-read play. The Miami defense collapsed on Gore in the middle of the field, while Kaepernick bounced around the left end and loped — there's no other way to describe those real-estate-eating strides — 50 yards to the end zone for a touchdown, delivering a 27-13 victory that was not particularly smooth, and certainly not easy.

"Great play call by Greg (Roman, the San Francisco offensive coordinator)," coach Jim Harbaugh said. "That was a big third-down conversion. We've been struggling on third downs, and to get that one to pop and get the touchdown was great. It was better than having to punt it back to Miami at that point."

The game was scoreless through the first quarter, and the 49ers (9-3-1) led just 6-3 at halftime. They finally gained some separation in the second half when they went largely to a shotgun formation and pistol offensive scheme, and when they forced Ryan Tannehill, the Dolphins' rookie quarterback, to play from a deficit and throw deeper passes. Miami wound up with just 227 yards of total offense.

The Dolphins (5-8) showed some pluck by mounting a 13-play, 75-yard drive in a hostile West coast venue to draw within seven in the fourth quarter. They capped the march in spectacular fashion, too, tight end Anthony Fasano sprawling headlong and reeling in Tannehill's pass with one paw, then landing on his knee to convert a fourth-and-goal play from the 3-yard line.

The next two times the Dolphins got the ball, though, they turned it over on fourth-down incompletions from Tannehill.

In between, Kaepernick got loose for the longest scoring run of his short career. The second-year QB completed 18 of 23 passes for 185 yards when he wasn't being harried by Cameron Wake. The Miami defensive end sacked Kaepernick three times, beating right tackle Anthony Davis badly in the first half. Jared Odrick also had a sack for the Dolphins.

"We got beat a couple times, just beat up front," Harbaugh said. "And one time, for sure, (Kaepernick) got fooled by the coverage."

All the scoring came on special teams before halftime, with David Akers outdueling Miami kicker Dan Carpenter two field goals to one. The play that changed the game also came on a kick.

The Dolphins forced the Niners to go three-and-out on their first possession of the second half. But Marcus Thigpen muffed Andy Lee's punt, and C.J. Spillman recovered for San Francisco at the Miami 9-yard line.

"I felt a couple of guys close to me, and I tried to take a peek at the last minute," Thigpen said. "Then I looked back up and lost the ball."

Two plays later, Gore took it in from a yard out, giving the 49ers a 13-3 lead.

The Dolphins answered with a field goal, but a weaving 19-yard run by Gore on the first play of the fourth quarter set up Anthony Dixon for a 1-yard plunge of his own, and the 49ers had a little breathing room.

That 13-play, 83-yard scoring drive was kept alive by a pass interference call on Miami cornerback Sean Smith on a fourth-and-1 play from the Dolphins' 38-yard line.

The Niners had some ill-timed flags of their own, including a roughing-the-passer call on linebacker NaVorro Bowman that gave Miami a first down the San Francisco 35-yard line with 4:35 left. All in all, this victory was a little ragged around the edges. That's sort of the way things have gone this season — and probably how they'll continue to go, which doesn't matter much to the 49ers as long as they're winning.

"We're getting everybody's best effort," Niners safety Donte Whitner said about opposing teams. "It really doesn't matter who we play, we're getting everybody's best effort each and every Sunday, and they use us to measure themselves. St. Louis did the same thing. Every team we've been playing all year has been giving us their best shot, so these next three games aren't gonna be easy."

Starting with a whopper next weekend: a trip to New England to play the Patriots on Sunday night.

You can reach Staff Writer Phil Barber at 521-5263 or phil.barber@pressdemocrat.com.