The Texans led 21-0 at halftime, then withstood a rally led by backup quarterback Alex Smith. Eugene Wilson's interception on fourth down halted the 49ers' last-gasp drive.
San Francisco (3-3) benched Shaun Hill after a terrible first half, and Smith threw three touchdowns to Vernon Davis to close the gap. Michael Crabtree started in his NFL debut and had five receptions for 56 yards.
The Texans (4-3) won consecutive games for the first time this season.
Davis' third touchdown came on a 23-yard reception got San Francisco within 24-21 with 3:49 remaining. Houston's only second-half points came on a 50-yard field goal by Kris Brown with about 7 1/2 minutes left.
Crabtree's first game after ending a 71-day contract holdout Oct. 7 was overshadowed by the quarterback shakeup. In his first game since 2007, Smith sparked San Francisco's offense in the second half and was 15 of 22 for 206 yards. The former No. 1 overall draft pick made only one major blunder -- the desperation throw that Wilson picked off.
His first touchdown came on a 29-yard pass to Davis that made it 21-7, finishing a drive that included a 17-yard reception by Crabtree.
Smith found Davis again on a 14-yard touchdown pass to cut the lead to 21-14 with about 13 minutes remaining. That score came 10 plays after Dashon Goldson forced and recovered a fumble by Slaton late in the third quarter. It was the fourth fumble he's lost this season after losing two as a rookie last year.
Davis finished with seven receptions for 93 yards.
Matt Schaub's 42-yard touchdown pass to Owen Daniels made it 21-0 in the second quarter, and Slaton's second score came on a 9-yard reception in the second that made it 14-0. The Texans got the ball back when Arnaz Battle muffed a punt that Andre' Davis recovered at the 10.
Schaub was 20 of 30 for 264 yards and two touchdowns.
Slaton put Houston ahead 7-0 with a 1-yard run in the first quarter. That play was set up when he made an 11-yard catch and run on third-and-9.
The 49ers couldn't get anything going in the first quarter, netting just 1 yard as Hill was constantly hurried by the Texans defensive front. Mario Williams, who's been slowed by a sore shoulder, got his first sack since Oct. 4 when he took down Hill on San Francisco's first offensive play.
Things weren't much better in the second quarter and the 49ers had managed only 50 yards and three first downs by halftime. It was the fewest yards the Texans have allowed in the first half in franchise history.
The only highlight in the first half for San Francisco came when Hill found Crabtree near the sideline for an 18-yard reception in the second quarter. But it seemed like every good play was canceled out by a bad: San Francisco went for it on fourth-and-2 late in the quarter and Hill's hurried pass was broken up by Brian Cushing.
Hill finished just 6 of 11 for 45 yards.
sp The Texans were without Andre Johnson on their last drive after the star receiver injured his chest earlier in the fourth quarter. He returned for one play but was escorted off the field before the game ended after a long discussion with Houston's medical staff.