Stock markets closed with marginal gains on Thursday after a day of fluctuating performance.