* Sells 7.86 percent stake at 20.10 euros per share
* HSBC declares 5.37 percent stake in Repsol
* Shareholder Sacyr may sell up to 4 percent - analysts
* Repsol shares close 3.6 pct lower (Updates with Pemex statement, updates share price, market comparisons, adds graphic)
By Tracy Rucinski and Ana Isabel Martinez
MADRID/MEXICO CITY, June 4 Mexico's state oil giant Pemex said it has sold most of its stake in Spain's Repsol SA for 2.09 billion euros ($2.9 billion) on Wednesday, and plans to unload the rest soon, ending a quarter-century partnership and freeing up cash to invest in its own energy sector.
Pemex sold 7.86 percent of Repsol to unspecified private investors at 20.10 euros each, a 3.7 percent discount to the Spanish company's closing price on Tuesday. The Mexican company later said it aimed to sell its remaining 1.4 percent stake in Repsol in August.
Pemex's exit as one of Repsol's top three shareholders ends a relationship that had become increasingly fractious in recent years due to disagreements on policies ranging from top management to the handling of Repsol investments in Argentina.
Pemex had publicly criticized Repsol Chairman Antonio Brufau's compensation and management, in particular his handling of the Argentine government's move to nationalize Repsol's YPF unit. The Mexican company sought to oust Brufau in 2011.
Tensions reached breaking point last month when Brufau appointed a chief executive officer whom Pemex had not endorsed.
"The decision to end the Repsol investment is due to the low profitability of the shares obtained by the current administration compared with other oil firms and our differences on its corporate governance," Pemex said in a statement.
Pemex said it had made a more than $900 million gain on its decades-long investment.
Repsol said in a statement that Pemex had resigned from its board seat.
The sale comes five days before Mexican President Enrique Pena Nieto makes his first official visit to Spain and as the Latin American country is putting an end to the oil and gas production monopoly Pemex has had since 1938.
Two sources close to the matter said that the books were not covered, leaving bookrunners Citigroup Inc and Deutsche Bank AG holding an unspecified number of shares.
Separately, HSBC Holdings PLC declared a 5.37 percent stake in Repsol worth 1.4 billion euros on Tuesday, becoming the oil firm's fourth-largest shareholder behind lender CaixaBank SA, builder Sacyr SA and Singapore's Temasek Holdings Ltd.
Shares in Repsol closed 3.62 percent lower on Wednesday at 20.11 euros, ending around the price of the placement.
Shares in Repsol have risen less than 30 percent since the end of October 2004, when Brufau took over, compared with a more than 130 percent increase in the NYSE Arca Oil Index of leading oil companies.
Pemex Chief Financial Officer Mario Beauregard said the company would look to sell its remaining shares in due course. "Once this financing is complete, we'll look for the best market conditions to get rid of the remaining position," he said.
The plan was to sell the shares in August, Beauregard said, confirming what company sources had earlier told Reuters.
Pemex said it would remain open to making investments in other companies if opportunities arose.
Repsol analysts welcomed the departure of Pemex, saying it should help ease boardroom tensions at Repsol and allow Brufau to focus on acquisition plans.
"We believe that this gives Repsol management a free rein to decide on the next strategic steps for the company," Exane BNP Paribas analyst Alejandro Demichelis said.
Repsol has raised $6.3 billion from its exit from Argentina after that country seized the oil major's YPF unit in 2012. It could spend part of the funds boosting growth at its upstream business, with a search for cash-generating oil assets.
It will also pay a 1 euro-per-share special dividend with part of the Argentine proceeds on June 6.
After being left with a significant Repsol holding, Citi and Deutsche Bank said in a regulatory filing a "guarantee" on the deal had come into effect, without giving details on what type.
Deutsche Bank and Citigroup declined to comment.
The banks' position in Repsol may be as much as 6 percent, one banking source said.
Analysts said they do not rule out its second-largest shareholder, Sacyr, selling up to 4 percent of its Repsol stake in coming months as it tries to refinance a 2.4 billion-euro loan linked to its Repsol purchase before the start of 2015. ($1 = 0.7342 euros) (Additional reporting by Freya Berry in London, David Alire Garcia and Michael O'Boyle in Mexico City and Carlos Ruano in Madrid; Editing by David Holmes, Mark Heinrich, Jane Baird, Marguerita Choy, Simon Gardner and Jonathan Oatis)