Winger Hammill, signed from Liverpool in August, turned to lash home the winner from an acute angle in the 74th minute after Barnsley had spent most of the match defending as if their lives depended on it.
Barnsley, who handed the manager's post to former Rotherham boss Robins in September, have won their last three league matches and success against Doncaster lifts them above their South Yorkshire rivals in the table.
Sean O'Driscoll's Doncaster side, decimated by injuries, had drawn their last four league matches, but have won only one league game this season, which came at the end of August.
Chances were limited in a fast and furious opening 20 minutes.
Doncaster defender James Chambers has his shot blocked by Barnsley's Darren Moore and James Coppinger, who shook off an ankle injury to start, saw a header well saved by visiting goalkeeper Luke Steele, back in the Tykes' starting line-up for the first time since the end of August.
Doncaster enjoyed the lion's share of possession and went close to breaking the deadlock when Jason Shackell headed Martin Woods' corner over the crossbar in the 24th minute.
Woods then drilled in a left-footed shot from 20 yards that flew wide, but neither side could carve out a clear-cut opening before the Rovers midfielder went even closer just before the interval.
The Scot collected the ball on the edge of the penalty area and unleashed a goalbound shot that Steele brilliantly saved at full stretch to keep the visitors level.
Doncaster swept forward after the restart and Billy Sharp's angled shot was eventually cleared by Barnsley's on-loan defender Ryan Shotton.
Dean Shiels went even closer for Rovers soon after when his sweetly-struck right-footed shot hit the crossbar with Steele comfortably beaten.
The Tykes goalkeeper did well to hold Sharp's close-range drive as Doncaster strove to break the deadlock, while Shiels fired in a left-footed shot from 20 yards.
Barnsley had spent long periods pressed back in their own half in the second period, but broke forward in the 68th minute and almost snatched an unlikely lead through Hammill, whose shot was caught by home goalkeeper Neil Sullivan.
Doncaster responded again and Coppinger was inches wide with his fizzing drive, but the home faithful were stunned by a Barnsley goal from nowhere.
Iain Hume clipped in a corner and Hammill created space to unleash an unstoppable shot beyond Sullivan.
Barnsley defender Stephen Foster then headed another Hume corner straight at Sullivan and was inches wide with another effort soon afterwards.
Second-half Barnsley substitute Anderson De Silva went close from close range and aside from Coppinger's speculative late shot from 30 yards, Doncaster, in command for most of the match, were spent.