The two sides are locked together at the top of the East of Scotland Premier Division standings, but it was the visitors who took the cup honours and booked a second round tie at Lowland League side Broomhill.
Penicuik came close to opening the scoring in the eighth minute when a Ryan Baptie free-kick fell to Jack Furness, but his strike was brilliantly saved by keeper Ally Adams.
Robbie Neave then had a netbound shot deflected for a corner but Penicuik did take the lead in the 24th minute when Andy Forbes rose to power home a header from another Baptie free-kick.
Only a last-gasp tackle denied Nicky reid the chance to double the lead, but Tranent responded and the alert Robert Watt had to get down quickly to save and keep his side in front.
Tranent were now applying constant pressure on the home defence but it looked as if Cuikie would hold out until half-time.
However Tranent were awarded a penalty a minute into stoppage time and Craig Stevenson sent Watt the wrong way from the spot.
Early in the second half a deflected Reid effort was tipped away by Adams at full stretch and Aaron Somerville free with only the keeper to beat.
But on the hour mark there was panic in the Penicuik defence when three attempts on goal were desperately blocked.
In the 72nd minute Somerville's cross was weakly headed into the arms of Adams by Scott McCrory-Irving.
And they were punished for that miss when Tranent took the lead three minutes later, Jamie Docherty lashing the ball into the top corner after keeper Watt had made two great saves.
A long-range free-kick flashed inches past the Penicuik upright before the home side's fate was sealed five minutes from time when Ben Miller slotted home at the second attempt.