Both sides had a good go at breaking the deadlock in a goalless first half with Boro going close early and late in the half and Rovers enjoying a good spell in the middle.
Wilson Kneeshaw turned sharply to fire just wide before a Williams free-kick came back off the bar and Matthew Waters pulled a follow-up effort just wide.
Adam Reach, returning from an ankle injury, headed at Rovers goalkeeper Seb Usai as he met a left-wing cross from Jordan Jones, then Usai was forced into a flying save to turn away another Williams free-kick.
But Rovers settled and Fabio Nunes curled just over from the edge of the penalty area.
That set up a period of pressure where Jamie Maclaren went close and Connor Ripley produced fine save to deny Leon Best, tipping a 20-yard effort onto a post.
The impressive Raheem Hanley saw an edge-of-area volley deflected just wide.
In the final minute of the half Ryan Brobbel crossed from the right, Kneeshaw met it well and it looked as though his shot would put in the lead but Usai produced a terrific save.
Half-time: Boro 0 Rovers 0
Rovers started the second half brightly and Maclaren had a drive from the edge of the area tipped over.
With 55 minutes played the game eventually rendered its first goal as Hanley played a neat ball to Leon Best and the former Newcastle striker, playing just his second game in seven months following knee surgery, controlled well and tucked a low shot under Ripley from 12 yards.
It was a lead that lasted four minutes before Luke Williams lashed an unstoppable shot past Usai from around 30 yards out.
He followed up that effort with another which curled just wide before Kneeshaw shot just over from the edge of the penalty area.
Brazilian Marcos Rhoden was introduced with 10 minutes remaining, just enough time to demonstrate a nice touch on the ball.
Then with 82 minutes of a competitive game played Boro edged in front when Luke Williams saw a 22-yard curling effort beat Usai with aid of a deflection.
In the second minute of injury-time substitute Bradley Fewster delivered a decent cross from the right, Jordan Jones' shot was saved but handled by an outfield player.
Luke Williams stepped up to complete his hat-trick, although that came as he followed-up his penalty, which was saved.
Boro: Ripley, Bennett, Reach, Waters (Rhoden 79), Burgess (Capt), Jackson, Brobbel (Fewster 79), Morris, L Williams, Kneeshaw, Jones. Subs: Montgomery (gk); Sirrell, Burn. Order of subs: 1) Fewster 2) Rhoden.
Rovers: Usai; Wylie, Beesley, O'Sullivan, Fernandez Molina, Daly (Capt), Maclaren, Hanley, Forrester, Best (Hands 77), Nunes (Pero 77). Subs: Payne, Urwin, Brown. Order of subs: 1) Hands 2) Pero.