He fell one vote short on the 14th ballot, but in the 15th and final round the tally was 216-212. The Democrats voted for leader Hakeem Jeffries.
In a tweet after the result, McCarthy said: "I hope one thing is clear after this week: I will never give up. And I will never give up for you, the American people."
Nancy Pelosi, former speaker of the House of Representatives, is expected to hand over the lectern to McCarthy on Monday.
More about: