Trent Richardson scored a 1-yard touchdown on his first carry in his Colts debut after being acquired on Wednesday from the Browns. He was drafted two spots behind top pick Luck at No. 3 last year.
Ahmad Bradshaw added a 1-yard TD run in the final minutes, and Adam Vinatieri kicked a pair of field goals before missing a 51-yarder early in the fourth.
But Luck came through again to give Indianapolis (2-1) more opportunities.
It was Colin Kaepernick's his first home loss at Candlestick Park as a starter.
Frank Gore ran for 82 yards after going for 60 total in his first two games, but there were few bright spots for Kaepernick as San Francisco (1-2) struggled to establish a passing game with tight end Vernon Davis sidelined by a hamstring injury. The defense committed numerous costly penalties for the third straight week.
Luck completed his initial six passes and spoiled the home team's reunion day between coaches on both sides who know each other's tendencies dating to their days at Stanford.
The Colts sure appeared to be better prepared.
Luck sent third-year coach Harbaugh to consecutive losses for the first time.
Before Sunday, he was 7-0 following a defeat — and Harbaugh faced criticism for playing and starting Aldon Smith two days after the linebacker was arrested and jailed on suspicion of driving under the influence and marijuana possession.
Kaepernick had been 4-0 on his home field since becoming a starter last November. This time, his 49ers were 10½-point favorites, but looked nothing like the better team in getting thoroughly outplayed.
He completed 2 of his 8 first-half passes and wound up just 13 for 27 for 150 yards with an interception and three sacks.
The 49ers had six penalties for 48 yards and didn't look much better than they did in their five-turnover, 29-3 loss at division rival Seattle last week.
Richardson carried 13 times for 35 yards, giving the Colts quite the 1-2 punch with Bradshaw's 19 rushes for 95 yards.
Luck took advantage of a pair of penalties by San Francisco to open Indianapolis' seven-play, 80-yard scoring drive over the opening 3:40 for a 7-0 lead.
On the Colts' first play from scrimmage, safety Donte Whitner received a 15-yard unnecessary roughness penalty when he led with his helmet on a hit against Bradshaw after a 3-yard catch. Tarell Brown was flagged for pass interference on the next play.
Another penalty on Brown, for holding, gave the Colts first down at the 23 midway through the second quarter and set up Vinatieri's 43-yard field goal. He kicked a 41-yarder in the third quarter.
Brown was penalized yet again for pass interference midway through the fourth.
The 49ers answered Indianapolis' initial score. Kendall Hunter ran for a 13-yard score after Gore got the drive going with three long runs — quickly topping his total from the first two games.
Luck kept drives going with clutch plays as the Colts won the third straight meeting with San Francisco.
On third-and-9 and throwing from near his end zone late in the third, Luck hit Reggie Wayne for a 25-yard gain in which Wayne broke through several tackle attempts.
All-Pro linebacker Patrick Willis went down on the play and was helped off the field moments later with a groin injury that sidelined him for the rest of the game.
The Colts busted through San Francisco's defensive line, which lost nose tackle Ian Williams to a season-ending ankle injury last week.