I recently had to do something very similar (using a door as a headboard, mounted to the wall). I ended up using these nifty little flush hooks.
They will leave your shelf 2-3 mm away from the wall, but if you have a router, you can rout a recess to mount the hooks in the back of yourself, which will make it completely flush.
They take pretty small screws, but appear to be able to take a lot of weight. There's not a lot of movement in how they slot together, so I would only use two, to avoid alignment problems.
When I used them, I mounted the hooks onto my headboard, then put double-sided tape onto the other hook plate. I then used a tiny bit of blu-tac to hold the other hook plate (the one with the double-sided tape on it) in place. I could then just line it all up, push it onto the wall (so the tape grabbed) then slide the headboard up. You're then left with the other plates stuck to the wall in exactly the right place. You can then mark the drill holes you'll need to make easily and exactly. This is harder to explain than it is to actually do!