Scotland's population has been projected to rise over the next 15 years before falling below five million.
Concerns have been raised about a falling population
A report published by the registrar general predicts a population falling below five million in 2036 - 19 years later than previously thought.
Registrar General Duncan Macniven said more births, fewer deaths and immigration were behind the rise.
However, he said figures suggested Scotland would become an ageing nation.
The figures showed Scotland's population would be expected to peak at 5.1million in 2019 and then slowly decline, reaching 4.86 million by 2044.
The number of people of working age has been predicted to fall by 7% from 3.18 million in 2004 to 2.96 million in 2031.
The number of people of pensionable age has been expected to rise by 35% from 0.97 million in 2004 to 1.31 million in 2031.
Mr Macniven said that, despite the figures, Scotland was not facing a population crisis.
"Scotland's population is predicted to rise over the next 15 years thanks to slightly more births, slightly fewer deaths and more people coming to Scotland than leaving," he said.
"But we will still be an ageing nation because our birth rate has declined since the 1980s and our population is likely to fall from 2020, while the rest of the UK is on a rising trend."
Mr Macniven said the Scottish Executive could take action on overall population numbers, but it was powerless to address the age trend - it could only try and reduce its impact.
"There's no way you can fill in the decline in the working age population by heaping in more migrants and if you did you would merely be postponing the problem to the point where they age," he said.
"You're not going to fix the problem of the change in the age structure because that's the effect of the baby booms of the past."
First Minister Jack McConnell said the figures undermined the idea that there was "a brain drain" in Scotland.
He said: "They dispel the notion that we're haemorrhaging talent and they recognise that Scotland is an attractive place in which to live and work.
"But we have to build on that and ensure more people are attracted here in the future, or the population decline will return and we'll see that population age at the same time."
Mr McConnell called on people in Scotland to get behind the executive's efforts to attract new people to live and work in Scotland.
The Scottish Conservatives said the figures offered little reassurance.
The party's enterprise spokesman, Murdo Fraser, said: "Whilst these figures show a modest population rise up to 2020, there is no comfort in the long-term as we can see a steady decline to under five million by 2036.
"All the evidence shows that the underlying factor for any population is the strength of the economy.
"Where it is strong, people have greater self confidence and larger families. A country with a strong economy will also attract immigrants."
The Scottish National Party said the figures proved the executive had failed to grasp the severity of the population problem.
SNP enterprise spokesman Jim Mather said: "Even the registrar general does not believe that current measures are enough to sustain the current population.
"Alarm bells should be ringing when our population is likely to fall from 2020, while the rest of the UK is on a rising trend.
"If we are serious about facing up to this challenge, Holyrood must be given the powers needed to effect positive change."