Goals by Tobin Heath, Carli Lloyd and Alex Morgan provided the smallest margin of victory for the United States in the tournament, having won three previous games by a combined 31-0.
"I always say I respect the game and the opposition and this is the proof," U.S. coach Pia Sundhage said. "You can't take anything for granted. We score a lot of goals, but Costa Rica, they came out and played well, played hard.
"Eventually, we had three goals. Despite the first goal fell in quickly, we struggled a little bit. It became an emotional game more so than a tactical one."
The three-time and defending Olympic champion Americans earned the first of two berths from CONCACAF, with Canada claiming the other later Friday with a 3-1 win over Mexico.
It was the eighth victory for the Americans in eight meetings against the Ticas, but it matched the closest the Costa Ricans had come to the United States in 11½ years of matches. Twice before Costa Rica had lost 3-0 to the United States - in 2008 and 2010.
Heath gave the United States the lead in the 17th, Lloyd scored her sixth goal to add the insurance in the 72nd and Morgan completed the tally in the 89th.
Costa Rica started brightly with high pressure that disrupted the United States' possession in the first half. It hit the bar midway through the first half, but appeared to fatigue in the second half, when the Americans repeatedly threatened the Ticas goal.
"They (Costa Rica) ran out of gas, which changed the game," Sundhage said.
Heath scored her second of the tournament and fifth in her international career when Lauren Cheney curled a corner kick to the far post. Abby Wambach bounced a header that keeper Erika Miranda palmed away but right to Heath, who headed it in.
Miranda was one of five changes Costa Rica coach Karla Aleman made to her starting lineup from Monday's match against Canada. Besides getting Katherine Alvarado back from her concussion suffered in the group stage and Daniela Cruz from a knee injury, Miranda replaced Julieth Arias, who had played the first three matches but allowed a howler in the loss to Canada.
The United States had the better of play in the remainder of the first half, but Costa Rica had its chances. U.S. goalkeeper Hope Solo had to race off her line to the top her penalty area to block a sprinting Raquel Rodriguez in the 25th, and Cruz unleashed a 35-meter effort that struck the intersection of the post and crossbar in the 27th.
The Americans, however, began to dominate in the second half, generating four corner kicks in the first 14 minutes.
"For Costa Rica Rica it has been a great honor of playing against the best team in the world at the moment," Costa Rica coach Karla Aleman said. "These are the kind of experiences, the ones that we need for Costa Rica to get better and grow."
The United States doubled the lead when Wambach was able to break in alone on Miranda and attempted to chip a ball over her. Miranda was just able to get a hand to it and Cruz was able to clear off the line. But the ball went to Lloyd, who shot from the top of the area past a scrambling Costa Rica defense.
Morgan, who replaced Heath in the 63rd capitalized on a clearly tiring Costa Rica defense, beating both Wendy Acosta and Carol Sanchez to the byline, turning inside and flicking past Miranda.
The United States, which had only two shots on goal by the 27th minute, outshot Costa Rica 21-7 in the final tally and continued its streak of being the only team from CONCACAF to play in every Olympics since women's soccer was introduced in 1996.