Stocks closed modestly higher, helped by a rally in tech shares, but investors were rattled as a sharp drop in crude-oil prices hit energy shares.

The 2% decline in crude on the New York Mercantile Exchange prompted selling of energy-related stocks and spoiled what was shaping up to be a strong day, particularly for the downtrodden tech sector.