Witham were denied by two great saves from Margate goalkeeper Nikki Bull as they went down 2-1 at Hartsdown Park.
Ryan Moss grabbed an early opener and Tom Phipp flicked in a second as the home side led 2-0 at the break.
But that did not tell the whole story as Witham attacked with real threat.
And after the break – with the home side forced into a defensive reshuffle – the visitors got back in it through Kurt Smith's goal.
Bull made brilliant stops from Smith and Lee Townrow to keep Gate ahead and in the end, it was a relief to the majority of the 560-strong crowd when the final whistle went.
Margate left Charlie Allen and Luke Moore on the bench, with Phipp and Luke Williams starting, and they were then forced into a last-minute change as well when Brett Johnson had to be replaced by Jamie Stuart.
The first effort on goal was a howitzer from Phipp which only just went over the top, before Shami Pedulu headed straight at Bull.
But on five minutes, the deadlock was broken after some brilliant work from Williams, who skipped around his man before shooting from the edge of the box. Martyn Guest dived at full-stretch to keep it out but it was Moss who got their first to side-foot home his eighth goal of the season.
If anyone expected Witham to roll over, they were very much mistaken and Garry Kimble's side continued to press, with their proficiency in the wide areas clear for all to see. One cross forced Bull into a save, a second was well headed out by Anthony Riviere and a third fell to Lewis Godbold, who blasted wide from eight yards.
Kane Wills saw a long-range effort force Guest into a fine save after a slight deflection, and from the resulting corner Phipp hooked the ball just over the top.
Pedulu was forced off on 20 minutes having suffered a knock earlier on, with James Robinson the man to replace him.
Lewis Taylor ended a marvellous Margate move by firing a low shot wide from 30 yards before Bull – who won August's Golden Glove – had a moment of panic as Robinson almost charged down his clearance.
Another corner presented Phipp with another chance, this one cleared out to Jack Bennett who crossed it back in for a looping header from the former Ebbsfleet United man which Guest caught under pressure.
But Witham should have been level just before the half-hour when Joshua Mayhew was presented with the ball in the centre circle and he picked out the perfect pass to Tom Wraight on the left, who had time to pick his shot but fired it just over the top.
And he was made to pay for that miss as Gate extended their lead on 33 minutes. Zac Attwood's low cross was deflected into the side-netting and the corner was cleared out to Attwood, who shot acrobatically from 25 yards and it was diverted in by the head of Phipp.
Mayhew was injured in his attempt to clear and became the second Witham player to depart, leaving the field on a stretcher to be replaced by Smith, while Gate defender Charlie Wassmer also picked up a knock in the same incident.
Smith soon created a chance for Robinson but Phipp and Stuart got back to prevent the first Town sub getting his shot away.
Wassmer had to make way at the break with his injury, replaced by Adam Cash, meaning that Riviere moved into the centre of defence alongside Stuart, making the combined age of Gate's two centre-halves 72.
But Gate were undeterred and almost extended their lead when Attwood headed Taylor's cross against the outside of the post, before having shot blocked following a short Gate corner.
A dangerous Bennett free-kick was deflected behind and Stuart eventually fired the set-piece over the top – and then, in the blink of an eye, the deficit was halved.
A ball over the top was missed but Smith still had Bull to beat – and he did so brilliantly, acrobatically thumping a volley into the top corner.
Gate's defensive shortage threatened to make it an uncomfortable final half an hour, but they almost restored the two-goal gap after a scintillating move involving Riviere, Phipp, Taylor and Cash ended with the latter crossing for Moss, but his shot was blocked by Townrow.
Smith headed over the top as Witham pushed, and Bull had to deflect Wraight's effort wide after a lapse in concentration.
Attwood's low cross was just diverted past his own post by John Watson and it was real end-to-end stuff for a while, with James Stevens' corner somehow missing everyone in the middle as the visitors looked for a leveller.
Smith was brilliantly denied a second by Bull's feet, before the Gate stopper made an even better save, clawing away Townrow's header just as it looked to be creeping inside the far post.
Both sides had attacks but neither were able to find the key pass as Margate held on for their eighth win in 10 outings so far this season.