Luiz Adriano and Alex Teixeira also scored for the Ukrainian side to claim a victory, which combined with Manchester United's 5-0 defeat of Bayer Leverkusen in Germany, moved Shakhtar up into second place in Group A.
The defeat for Sociedad means they are destined to finish fourth in the group, thereby ending their European campaign this season. Shakhtar's fate remains in their own hands.
The Spanish side started off the brightest, though, in a game which had a must-win status for them thanks to the single point they had earned in the group stages from the goalless draw against Manchester United earlier this month.
It was Shakhtar's Brazilian winger Bernard who had the first attempt on goal, when he jinked his way to the left by-line before his shot was comfortably caught by Claudio Bravo.
Teixeira then tested bravo with a fierce shot which the Sociedad keeper kept out with his legs before his opposite number Andrei Pyatov saw his first taste of the action when he comfortably gathered Chori Castro's effort from distance.
Teixeira tried his luck with a speculative 30-yard half-volley which flew over the bar before Darijo Srna came the closes to breaking the deadlock in the 35th minute when his swerving strike hit the crossbar.
However, Shakhtar were ahead two minutes later through Adriano, who was on hand at the near post to poke the ball in from a corner.
Inigo Martinez went close to levelling before the break when he somehow put the ball over the bar after finding space in the box.
The visitors' woes were compounded early in the second half thanks to striker Teixeira, as the Brazilian capitalised on a moment of hesitation by Martinez to score from the middle of the area.
Costa then made sure of the result when he fired into the top corner in the 68th minute after being set up by Teixeira before he added his second three minutes from the full-time whistle when he headed in at the back post.