Gretna's fairytale march to the Scottish Cup final ended with a cruel twist as Hearts snatched the trophy after a penalty shoot-out.
Hearts won the Scottish Cup for a seventh time
Rudi Skacel tucked away a close-range strike six minutes before the break to give Hearts a deserved lead.
Gretna roared back, equalising when Ryan McGuffie hit home the rebound after his penalty kick was saved.
But Hearts, who had Paul Hartley sent off, triumphed when Derek Townsley and Gavin Skelton missed from the spot.
Gretna have had things all their own way this season as they wrapped up a second successive promotion at a canter and reached the final without meeting a top flight side.
But their collection of gnarled veterans ran out of luck at Hampden after giving Hearts an almighty scare.
Hearts enjoyed the best chances and were denied what looked a clear penalty in extra-time and saw Hartley sent off in the 120th minute.
And Steven Pressley, Robbie Neilson, Skacel and Michal Pospisil scored the decisive spot-kicks, while Craig Gordon saved Townsley's weak effort and Skelton hit the crossbar.
A penalty shoot-out looked unlikely after Hearts made an impressive start to the match.
Hearts had Gretna pinned back in the opening minutes and were denied an early lead when a low, 20-yard strike from Deividas Cesnauskis cannoned off the post.
Gretna goalkeeper Alan Main then made smart saves to deny Skacel and Edgaras Jankauskas.
There was a brief respite for the Second Division champions when Skelton sent in a screaming free-kick that Gordon got fingertips to.
The same player then did well to tackle Skacel on the edge of his own penalty area as the Hearts midfielder broke from the half-way line.
Bednar failed to make the most of a free header at the back post and Main clawed away the striker's tame effort.
But the breakthrough came for Hearts after 39 minutes when Skacel chose a good time to end his 14-game scoring drought.
A long throw-in from Neilson reached the back post area after a touch from a defender and the Czech was on hand to drive home an angled shot from six yards.
Gretna were presented with a chance after the interval when the ball broke to Steve Tosh but he pulled his 18-yard shot wide.
Rowan Alexander's side were committing more men to attack and leaving gaps at the back but Hearts lacked their early fluency.
Bednar shot high and wide from 22 yards, with Main stranded yards from his goal-line, and his side almost paid for that miss minutes later.
Rudi Skacel was on target for Hearts
David Graham ran right through the Hearts defence and calmly rounded Gordon but Neilson got in a fantastic sliding challenge to deny the substitute an open goal.
However, Gretna were back on level terms after John O'Neil was tripped by Cesnauskis to win a 75th minute penalty kick.
McGuffie's spot-kick was saved by Gordon but the 25-year-old was able to stroke in the rebound.
Skacel was inches away from connecting with a ball across the goal face and Hartley shot over from 12 yards as Hearts pressed again.
Gretna defender Chris Innes sliced an 85th minute shot wide as the ball broke to him at the back-post and the game moved into extra time.
A deflected Skacel free-kick looped just wide and Jankauskas.missed the target with a header from six yards when played resumed.
Skacel then hit the post and Main made a super save to block a fierce Hartley effort.
With five minutes of extra time remaining, Skacel broke clear and pushed the ball beyond Main, who clipped the midfielder's heels.
The player stayed on his feet and was crowded out, while referee Dougie McDonald failed to spot what looked like a penalty.
Hartley, Hearts' main penalty taker, was then dismissed after picking up a second yellow card for for kicking out at Townsley.
But the Edinburgh side held their nerve to win the cup for the seventh time.
Hearts: Gordon, Neilson, Pressley, Tall, Fyssas, Cesnauskis (Mikoliunas 86), Aguiar (Brellier 72), Hartley, Skacel, Bednar (Pospisil 70), Jankauskas. Subs Not Used: Banks, Berra.
Goal: Skacel 39, Sent Off: Hartley (120) Booked: Skacel, Fyssas, Hartley
Gretna: Main, Birch, Townsley, Innes, Nicholls (Graham 55), McGuffie, Tosh, O'Neil, Skelton, Grady, Deuchar (McQuilken 103). Subs Not Used: Mathieson, Berkeley, Henderson.
Goal: McGuffie 76. Booked: Birch, Tosh.
Ref: D McDonald