Market benchmark indices Sensex and Nifty ended with marginal gains on Thursday after falling in the past three days, helped by fag-end buying in energy, telecom and utility stocks. Investors remained cautious amid renewed foreign fund outflows and prevailing risk-off sentiments, traders said.