Defeat at the Stadium of Light would have left the Black Cats in a perilous situation just above the relegation zone but, having gone behind to Jonathan Walters' early effort, they showed great heart in the second half and deserved to see O'Shea prod home a 63rd-minute corner.
Gardner can take little credit for the result, though, and his red card for a dangerous challenge on Charlie Adam was fully warranted.
Danny Rose almost stole victory for the hosts 10 minutes from time but his low shot hit the outside of the near post.
As it stands, Sunderland are now level with Norwich and Newcastle on 38 points - three ahead of a Wigan side who have a game in hand.
After a pre-match appearance by members of Sunderland's FA Cup-winning side of 1973 the opening moments yielded little of consequence, although Alfred N'Diaye won warm applause for a couple of firm challenges in midfield.
Indeed, neither side had managed a shot on goal before Walters put the visitors in front in the ninth minute.
Ryan Shotton's first long throw of the match played its part, with his delivery earning a corner. Adam took it and when Walters' header was blocked by Danny Graham on the line the Republic of Ireland international reacted quickest to power the loose ball home via the underside of the bar.
Sunderland had a chance to reply quickly when Dean Whitehead conceded a dangerous looking free-kick, but Gardner shot wide.
Adam and James McClean exchanged a couple of full-blooded challenges, but in terms of influence Adam was comfortably winning their head to head.
McClean was not having an impressive return to the starting XI, losing possession, tackling carelessly and dragging a 25th-minute shot well wide when passing seemed a better option.
Adam Johnson, playing off Graham, was Sunderland's best attacking outlet but he was struggling for support.
Sunderland's task became even harder when Adam's charge down the right flank was ended by a dangerous studs-up challenge from Gardner.
Referee Lee Mason was close in attendance and it was no surprise when he produced a red card.
The dismissal led to a reshuffle from Di Canio, who rarely moved from the edge of his technical area, with Jack Colback filling in at right back and Johnson withdrawing slightly to the wing.
Steven Nzonzi added to the increasingly feisty atmosphere with a needless trip on N'Diaye and was booked for his trouble.
Stoke did not overdo attempts to punish their opponents' numerical disadvantage and the Black Cats retained a decent amount of possession, albeit with little to show for it.
Johnson went closest to producing an equaliser with a free-kick just prior to the break but it squirmed wide.
Stoke swapped Marc Wilson for Geoff Cameron at half-time and the American almost gifted Sunderland a way back in with a heavy touch and weak clearance. McClean, inevitably, could not cash in.
But Sunderland had plenty of energy, not least Rose pushing up from full-back. Nevertheless, they might have been punished on the counter had Adam not met Walters' pass with an air shot in the 52nd minute.
O'Shea almost got on the scoresheet on the hour, prodding goalwards only to see Whitehead block on the line.
And the captain went one better two minutes later, popping up at the far post to side-foot Seb Larsson's corner over the line after Whitehead failed to get a solid connection.
The crowd roared their approval and manager Paolo Di Canio struck an imperious pose, fists clenched and arms out at his sides.
The Italian caught the eye moments later too, dashing angrily on to the pitch after the referee halted a Sunderland attack to allow Walters treatment.
Although Stoke appeared reasonably content with a draw chances still appeared, Whitehead striking well from the edge of the area to test Simon Mignolet.
Remarkably Sunderland's 10 men did not appear to tire and Rose was inches away from putting them ahead in the 80th minute with a skidding attempt that struck the outside of the post.
Nzonzi went closest to winning it for the visitors, hooking a shot over with his back to goal, but there was to be no late drama.