It was a storybook ending, just not the one the U.S. women’s soccer team wanted.

 In a shocking upset, Japan won the Women’s World Cup title on Sunday after it defeated the United States, 3-1, in a penalty kick shootout after the teams played to a 2-2 tie in regulation and extra time.

Japan twice erased one-goal deficits in the second half, the final time on a goal by Homare Sawa with about three minutes left in extra time. In the penalty kick shootout, Shannon Boxx, Carli Lloyd and Tobin Heath failed to convert for the U.S. women. With her team needing just one goal for the title, Saki Kumagai converted for Japan, whose players sprinted onto the field in celebration.

“Soccer is a funny game; unfortunately you can’t win everything,” said Ali Krieger, a Dumfries native and U.S. starting defender. “I think we played really good and I didn’t think they were going to tie it up on the second goal, I thought we would win 2-1, but that’s how it is.”

Ranked No. 4 in the world entering the tournament, Japan was arguably the sentimental favorite against the top-ranked Americans. It provided a feel-good story to a nation devastated by a March 11 earthquake and tsunami.

“They are playing for something bigger and better than the game and when you’re playing with so much emotion, and so much heart, that’s hard to play against,” U.S. goalkeeper Hope Solo said before the match.

But it was still a heartbreaking blow to the U.S. squad, which had captured the imagination of millions back home and was aiming for its first World Cup championship since 1999.

Before the first whistle, fans ranging from President Obama to Lance Armstrong tweeted their support. Meantime, Secretary of State Hillary Clinton ended a news conference in Athens by yelling, “Go USA!”

If there were one thing that characterized the U.S. team, it was its never-say-die attitude that helped it win a thrilling quarterfinal even after it fell behind Brazil in extra time — and down a player, no less.

But it was not enough on Sunday night even though the Americans dominated the first half of the match with powerful, possession-style play that created several scoring opportunities against what seemed like a lackluster Japanese attack.

 Despite dominating in the first half, the United States only landed on the scoreboard in the second. The opening goal came after 22-year-old Alex Morgan, the youngest player on the team, collected the ball off a sterling pass from Megan Rapinoe and kicked it past Japanese goalkeeper Ayumi Kaihori in the 69th minute.  

Japan equalized in the 81st minute, after Rachel Buehler and Krieger did not clear the ball out of the penalty area, which allowed Japan’s Aya Miyama to chip it past Solo. The goal gave Japan momentum, and it started to look a little more like the team that in the quarterfinals defeated Germany, the two-time defending World Cup champion and tournament favorite.

In extra time, Abby Wambach scored the go-ahead goal with a powerful header off a cross by Morgan in the 104th minute. With that goal, Wambach became the United States’ all-time leading scorer in a World Cup, beating the previous record of 12 goals by Michelle Akers. With only about 15 minutes left to play, it seemed as if the United States was set to win its third world title. 

But their hopes were dashed by a late goal by Sawa, the 32-year-old Japanese superstar who tied the score in the 117th minute to force the shootout.

“We fought hard until the end, until we won this game,” said Japan Coach Norio Sasaki, who said his team would celebrate with “one glass of excellent German beer.” 

Frankfurt proved it was a soccer-mad city, regardless of who was in the final, with thunderous applause repeatedly erupting from the sold-out crowd of 48,817 at Commerzbank-Arena, a stadium nestled in a forest area on the outskirts of the city.

The crowd gave Japan a standing ovation after the match.

 It was an emotional victory for Japan, which carried a banner reading: “To our Friends Around the World: Thank You for Your Support” after it clinched the title.

For the U.S. women, it was a decidedly different sort of emotion. 

“I think we gave the crowd a good game today . . . it has to be a final to remember,” said U.S. Coach Pia Sundhage, who praised her team for its style of play in the first half. “We won a silver medal. I hope I can feel that after a couple of weeks.”

Staff writer William Wan contributed to this report from Athens.