Ian McCall's men led at the break through Steven Bell's header but Pavol Safranko equalised with a superb volley and Paul McMullan drove the winning goal low past keeper Ross Doohan.
The victory moves Robbie Neilson's team to within five points of leaders Ross County, who host Partick Thistle on Saturday.