In your example, neither of the markdown heading IDs work as link targets; try https://beta.observablehq.com/@mootari/anchor-anchors-in-markdown#anchors-in-markdown and https://beta.observablehq.com/@mootari/anchor-anchors-in-markdown#before-headline.
Here’s my attempt at explaining what’s going on; anyone with a better understanding should feel free to jump in and correct me:
If you peek at the source of an Observable notebook, you’ll see that user code and content is running from a sandboxed iframe; there is also an associated div in the original page (with class “absolute w-100 black pen”) which contains divs (with class “relative”) for each of the cells.
Links to the IDs created in your notebook don’t work because those IDs label stuff inside the iframe. On the other hand, links to explicitly named cells work fine because named cells get IDs in the cell divs, and these live in the original page.