savingforcollege.com says you must make the withdrawal in the tax year the expense was incurred in order for it to be a qualified withdrawal:
https://www.savingforcollege.com/article/how-to-withdraw-money-from-your-529-planFrom the article:
"Step 2: Determine When to Withdraw
You should take 529 plan distributions during the same year you paid for the qualified expenses. For example, do not include second-semester tuition expenses that you paid for in December of the previous year.
It doesn’t matter if you withdraw funds in January for expenses not paid until August. Or if the withdrawal occurs in December for expenses previously paid during that year. Make sure they match up within the same calendar year, not the academic year.
If you withdraw the 529 money in December for a tuition bill that isn’t paid until January, you risk not having enough QHEE during the year of the 529 withdrawal. Likewise, if you take a distribution in January to pay for expenses from the previous December, that distribution will be non-qualified.
Towards year-end, 529 account owners should determine exactly how much was spent on qualified expenses during the year and make the appropriate “catch-up” distribution from the 529 plan. As part of this process, determine if the AOTC is maximized by paying second-semester college bills in December versus January."