David Mundell said discussions would “go to the wire” as the EU Withdrawal Bill reaches its final stages in the House of Lords.

His comments came as the UK Government launched a legal challenge to alternative Brexit bills passed by the Scottish and Welsh devolved administrations.

He told BBC Radio Scotland’s Good Morning Scotland programme: “I’m afraid the discussions and negotiations will go right up to the wire, but I am confident that we can reach an agreement.”

The Scottish and Welsh governments brought forward unprecedented legislation after a row with UK ministers over the return of devolved powers from Brussels once Britain leaves the EU.

Ministers in both Cardiff and Edinburgh have repeatedly branded the EU Withdrawal Bill a “power grab” which threatens devolution, and have refused to recommend legislative consent unless it is amended.

However bills passed in the Scottish Parliament and Welsh Assembly last month aimed at providing an alternative to the UK Government’s legislation have now been referred to the Supreme Court.

The decision was taken by the Attorney General and the Advocate General for Scotland, the UK Government’s senior law officers.

The court is being asked to rule on whether the legislation is constitutional and within the powers of the devolved legislatures.

Mr Mundell said: “The court referral is about seeking clarity, seeking certainty when different views have been expressed.”

He was unable to confirm the estimated cost of the legal challenge, adding: “We’re only at the very start of the legal process.

“It costs £200 to refer the case to the court. Obviously if the case became involved in a protracted discussion there is greater cost.”

He added: “It’s very important that when you see two different views expressed about a piece of legislation, that actually somebody gives a definitive view, is it competent or is it not.”