The striker scored the only goal of the game midway through the first half, smashing home a Paul Willis corner.
It was a nervy affair, with Spartans making life difficult for the League 2 outfit, but overall Rangers probably just about deserved the victory.
The win guarantees a last eight match against Hibernian at Easter Road on Sunday, March 8, a match which will be screened live on Sky TV.
The teams had drawn 1-1 at Ainslie Park in Edinburgh ten days earlier when Ally McKinnon bagged an injury-time equaliser to cancel out Paul Willis’s fourth minute opener.
Berwick were looking to book a last eight place for the first time since 1980 – ironically when they also faced Hibs – and Spartans, the reigning Lowland League champions, were aiming to become the first non-league side to reach the quarter-finals in 47 years.
Kick-off was delayed for 15 minutes to allow the crowd in and Spartans were cheered on by a large contingent in the 2,003 crowd, which was swelled by 11 bus-loads of supporters from the capital.
Both sides tested each other out in the early stages and Stevie Notman came to Berwick’s rescue with a block from William Bremner, amid appeals for a penalty. Kevin Motion also shot wide when Spartans broke down the left, whilst at the other end Devon Jacobs pulled a shot wide for Berwick.
Jack Beesley wasn’t too far away with a free kick which had Billy Bald scrambling across his line, and Jacobs burst forward at the other end, but his shot was easily taken by Spartans stand-in goalkeeper Blair Carswell.
Berwick were then forced into making an early substitution after only 26 minutes with Stevie Notman being replaced by David Gold, but their nerves were settled when the deadlock was broken after 28 minutes. Striker Lavery smashed the ball home from close range following a Paul Willis corner.
Gold skipped past two players but his shot from the edge of the box lacked conviction and at half-time it remained 1-0.
The second half got off to a slow start and Spartans tried to inject some lift into their play by making a double substitution after 58 minutes. Berwick also made a change with Blair Henderson replacing Ross Gray.
Berwick player-manager Colin Cameron, playing his third game in a week, was unlucky with a cross shot from the right which ended on the roof of the net and minutes later Willis had a shot tipped by Carswell onto the foot of a post.
The tension grew as Rangers knew they needed a second goal to give themselves some breathing space, and with their record of conceding a lot of late goals the nerves of the players, and the supporters, were beginning to tingle.
They had every reason to be. Spartans almost grabbed a late equaliser when Nixon clipped the top of the crossbar with a half volley from inside the box.
Berwick Rangers: Bald, Jacobs, Young, Fairbairn, Drummond, Notman (Gold 26), Gray (Henderson 68), Currie, Cameron, Lavery (Maxwell 80), Willis. Subs not used: Russell,Dargo, Andrews, Miller.
Spartans: Carswell, Nixon, Cennerazzo, Malone, MacKinnon, Brown, Beesley (Stevenson 74), Grant, Bremner, Sludden (Douglas 58), Motion (McLeod 58). Subs not used: Jackson, Thomson, Henretty, Sivewright.
Referee: Greg Aitken.