Stocks edged higher on Friday as rising energy shares overshadowed profit taking that followed the market's strong gains in March and April.

Exxon Mobil was up nearly 1 percent at $67.28 and the S&P energy index <.GSPE> gained 2.1 percent after oil rose above $53 a barrel on improved consumer confidence and evidence of compliance by OPEC with output cuts.

Earlier stocks fell but pared losses after stronger-than-expected data on consumer confidence and manufacturing in April.

The market's early losses came as investors chose to book some gains after stocks' strong run since the early March bear market low, The S&P 500 index recorded its biggest rise in nine years last month.

I think people are starting to feel they need to take profits on some on the gains they've had, said Angel Mata, managing director of listed trading at Stifel Nicolaus Capital Markets in Baltimore.

I wouldn't be surprised to see the psychology shifting from 'I don't want to miss the move' to 'I should be taking profits here.'

The Dow Jones industrial average <.DJI> added 7.41 points, or 0.09 percent, to 8,175.53. The Standard & Poor's 500 Index <.SPX> put on 1.48 points, or 0.17 percent, to 874.29. The Nasdaq Composite Index <.IXIC> was off 1.26 points, or 0.07 percent, to 1,716.04.

Consumer discretionary shares fell. McDonald's Corp was the biggest drag on the Dow after Goldman Sachs removed the hamburger chain from its conviction buy list, though it affirmed a positive rating on the stock.

McDonald's was down 1.7 percent at $52.38, while the S&P index of consumer discretionary shares <.GSPD> fell 1.1 percent.

Data showed the U.S. factory sector shrank further in April, but at a slower pace, and consumers felt more confident about the economy last month than at any time since September.

Also, new orders received by factories declined last month.

The Standard & Poor's 500 Index closed out its best month in nine years on Thursday, gaining 9.4 percent for April on hopes the worst was over for the financial sector and recession-hit economy. The index is up nearly 29 percent from the bear market low of early March.

MasterCard Inc , the world's second-largest credit card network, was among the laggards after it said revenues will grow less than expected in 2009 and that U.S. process volume continued to decline in April from first-quarter levels. MasterCard shares were down 7.3 percent at $170.03.

Shares of biotechnology companies weighed on the Nasdaq, with Celgene Corp down 6 percent at $40.16 after it reported quarterly results that were in line with guidance.

Of the 280 companies in the S&P 500 that have reported earnings to date, 65 percent topped analysts' estimates, according to data compiled by Thomson Reuters as of Thursday. However, many of the analysts' estimates had been reduced to reflect the current economic slump.

(Reporting by Leah Schnurr; additional reporting by Edward Krudy; Editing by Kenneth Barry)