I was able to resolve this issue by clearing the NuGet cache and restarting Visual Studio.
Here’s what I did:
In Visual Studio, I went to
Tools
→ Options
→ NuGet Package Manager
→ General
and clicked on "Clear All NuGet Cache(s)".
Then I restarted Visual Studio.
After doing this, the error disappeared and I was able to publish my project successfully. I can't believe I forgot to try that earlier.
Thanks to the user who suggested this solution on GitHub!