Gloucestershire 275 (Dent 53, Wright 7-53) and 348 for 6 (Cockbain 117, Lace 97) beat Leicestershire 421 (Hill 121, Evans 102, Harris 62, Mike 54, Worrall 5-79) and 201 for 9 dec (Higgins 5-62) by 4 wickets
Cockbain and Lace arrived just after lunch at 52 for 3 when winning the game was fanciful but they played calmly through to tea to leave 216 to win from 37 overs of the final session.
Their intent was immediate and they extended their stand to 224. Lace couldn't see the chase through and Cockbain fell for 117 so it was left to George Hankins to strike the winning runs.
It was the fourth-highest Championship run chase in Gloucestershire's history, a third victory to take them top of Group 2 and a remarkable turnaround having conceded a first-innings lead of 146.
Leicestershire were denied 22 overs on the third evening through bad light and rain and it denied them the chance to totally close the door on their hosts while giving them enough time to bowl them out again.
The Foxes left themselves 82 overs in the fourth innings and when the top three all fell cheaply, a first win of the season was possible.
But Lace and Cockbain batted through the afternoon to raise prospects of saving the game before having a dip in the final session.
Cockbain flicked Alex Evans to fine leg for four, pulled him over square leg for six and then pulled Chris Wright for four to raise a first first-class half-century since April 2016 - this being just his fifth game since then.
He skipped at Callum Parkinson's left-arm spin to lift him over deep midwicket for six and flat-batted Wright over long-off for another maximum to bring the target down to 100 from 17 overs before pulling Wright for two to raise his century in 166 balls with nine fours and three sixes. He finally fell skying a catch to mid-off but by then the game was effectively won.
Lace initially led the resistance after lunch. He straight-drove Wright for four to go to a second fifty of the season and was on the cusp of a maiden century for Gloucestershire before top-edging a pull from Parkinson to midwicket.
Leicestershire stumbled over their morning's work in being bowled out for 201 and, without the injured Dieter Klein, were powerless to stop the Glosters' evening charge.