JPMorgan Chase & Co surprised Wall Street by winning a leading role in Facebook's much anticipated public offering, besting other banks that have competed for months for the coveted position.

Morgan Stanley, JPMorgan and Goldman Sachs Group Inc got the lead roles in the IPO, while Bank of America Merrill Lynch, Barclays Capital and Allen & Co also landed underwriting roles.

The banks were told that they had all made the cut during a commitment meeting on Tuesday, two sources familiar with the situation said. Generally, banks assuming these roles are told months in advance of a company filing for an initial public offering.

Facebook submitted its IPO documents to the U.S. Securities and Exchange Commission on Wednesday. The company expects to raise targeted $5 billion.

Two other sources familiar with the matter said on Wednesday that JPMorgan's months of schmoozing with Facebook executives paid off in the end. JPMorgan is ranked No. 4 among banks that advised on U.S. technology IPOs in 2011.

JPMorgan's Chief Executive Jamie Dimon, veteran rainmaker Jimmy Lee and other senior executives courted Facebook's Chief Operating Officer Sheryl Sandberg and Chief Financial Officer David Ebersman over the last year to help pave the way for the offering. Ebersman is leading the IPO process, one of the sources said.

As recently as December, Dimon visited Facebook's headquarters in Palo Alto, California, to establish its position within the team of bookrunners, the sources said.

Lee has had a business relationship with Sandberg for years, which has allowed him to learn about Facebook's culture among senior executives, one of the sources said.

Both Dimon and Lee saw Facebook as one of the next Blue Chips, and JPMorgan stepped in early to build a commercial relationship, including spending millions of dollars to help Facebook build a data center, the source said.

The hard-won mandate is likely to bode well for JPMorgan's league table rankings.

Currently, JPMorgan is ranked after Morgan Stanley, Deutsche Bank and Goldman Sachs in the league tables for U.S. listed technology IPOs, according to Thomson Reuters data.

This counts because the banks may be doing the deal more for prestige than money. Facebook's IPO could set a new standard for how much investment banks are willing to lower their advisory fees to win big business [ID:nL2E8CQEDA].

Banks may offer their underwriting services for as little as 1 percent of gross proceeds, sources have said, compared with the 7 percent fee that smaller deals typically fetch, or the 2 or 3 percent that large deals tend to command.

The underwriters established their relationship with the social network company over the last year through investments and loans.

In December 2010 and January 2011, Goldman Sachs purchased 69.5 million shares of Facebook's Class A common stock for $1.45 billion, according to filing. In 2010 and 2011, Morgan Stanley purchased shares of Facebook's Class B common stock.

A year ago, Facebook entered into a credit agreement with five lenders, including Morgan Stanley, JPMorgan, Goldman Sachs, Bank of America Merrill Lynch, and Barclays to borrow up to $1.5 billion in revolving loans, according to the filing.

In September 2011, the credit agreement was amended to increase the borrowing capacity to $2.5 billion, the filing said.

(Reporting By Nadia Damouni; Additional reporting by Alistair Barr and Stephen Lacey; editing by Paritosh Bansal)