The amendment failed in a 209-214 vote, with 24 Republicans voting against it.
The provision from Rep. Vicky Hartzler was an amendment to the National Defense Authorization Act to block the military from paying for gender reassignment surgery or hormone therapy for both service members and their families.
In 2016, then-Defense Secretary Ash Carter ended the ban on transgender people being able to serve openly in the military.
Carter put in place a process to occur in stages, and Defense Secretary Jim Mattis was facing a July 1 deadline on whether to allow new recruits who were transgender.
At the urging of the military service leaders, Mattis announced that he was delaying that decision
for six months to study the issue further.
But Mattis called Hartzler on Thursday to urge her to withdraw the amendment from the defense bill, two senior congressional sources told CNN. A Pentagon official confirmed that the call occurred.
The amendment was one of the most politically charged of the 200 that were being considered in the House as it debated the massive $696 billion defense authorization bill, which sets Pentagon policy and authorizes spending levels.
Hartzler, a Missouri Republican, argued the Pentagon should not be paying for costly surgeries when it's struggling with tight budgets and readiness problems.
"There are many problems, but funding transition surgeries with tax dollars is problematic because the surgery is very costly," Hartzler said. "Surgical recovery time decreases the deployability of our soldiers, and funding transition surgeries means diverting money from other defense priorities.
Rep. Duncan Hunter, a California Republican and Marine veteran, said the amendment wasn't preventing transgender people from serving, but said any gender transition should happen before someone enlists.
"You're joining the US military. Choose what gender you are before you serve," Hunter said.
Democrats objected to the amendment as discriminatory and a step backwards from the repeal of "Don't ask, don't tell" that allowed gay and lesbian service members to serve openly.
Both House Minority Leader Nancy Pelosi and Minority Whip Steny Hoyer spoke out against the amendment.
"Make no mistake, the effect and the intent of this unjust and mean-spirited amendment is to ban patriotic Americans from serving our country," Pelosi said. "They are fighting to rip away the health care of thousands of brave service members."
In June, Hartzler proposed a different amendment when the armed services committee marked up the defense authorization bill. Hartzler's initial amendment would have rolled back the Obama-era policy allowing transgender service members to serve openly, which she said she was withdrawing until Mattis decided how he was going to address the policy.
The amendment that was defeated Thursday only dealt with the military covering medical costs.
"This is very narrowly tailored to say that no tax dollars would go to pay for the gender transition surgeries," Hartzler said before the vote.