If the model numbers are the same, they will be the same parts built to the same specs. Of course, there are production line variations.
If there are slight variations in the model numbers, they may be the same or they may be slightly different (typically slightly cheaper) to prevent exact comparison to the regular part as Daniel R. Hicks said in a comment. This is because a manufacturer can do a large order for a part specific to OEMs which may be branded some way or slightly cheaper to them or not come with certain accessories or warranties which the manufacturer sources from other manufacturers (which can cut some costs, and possibly increase or decrease quality).
This ability to do bulk purchasing is the primary way that complete bikes are cheaper (they're buying 10s of thousands of cranksets or whatever without retail packaging or manuals or stuff, when you're buying 1 in a nice box with a warranty pamphlet and installation instructions and stuff, along with a markup for the shop). Naturally, its convenient to give the OEM and Retail versions different model numbers to keep things straight (for example, the OEM version's warranty is typically through the complete bike manufacturer, not the part manufacturer and may be shorter than the retail version, and it keeps stock easy to keep track of). This can be substantial - If you walk into Radio Shack for lets say a LM741 op amp, they'll charge you at least $1.50 for one of them (they don't do bulk discounts). My usual retailer for these type of parts, Digi-Key, sells the same part (possibly made by a different manufacturer, but I know to be of essentially same quality) for $0.70 each without the retail packaging (just cardboard boxes filled with foam and the chips in them) if I buy just one at a time, and for $0.28 each if i buy in lots of 1000 or more. Thats around 19% of the price I would have paid at Radio Shack, for a bit different packaging and just getting it in bulk from a wholesaler (certainly, digi-key is marking this up as well. If I were a huge company, I could probably skip them and go straight to Texas Instruments or whatever, and order my 1 million op amps at an even lower rate!). The complete bikes also often ship essentially assembled correctly, so you further save money on the total cost of the bike by not having to pay as much time for a tech at the LBS to do things. This is known as "economies of scale". [To drive the point home: As Kibbee points out in a comment, this is why the "losing money" articles news sites often run on electronics are faulty - the sum of the costs of parts to a consumer whose only buying a few of each part will be higher than the cost of the device, but when you're buying large quantities of each part, the sum of the costs will be significantly less. Those articles do make for great click-bait though!]
To quote Surly on this: "Can I get the same stem/crankarm/saddle/handlebar as the one that came on my Surly complete bike?"
"No, we don't offer most of the stock parts that come on our complete bikes. These parts are sourced from the manufacturer directly and aren't usually available for purchase, but in most cases you can get something darn close or even better from your LBS"
(Surly is owned by QBP, so they can source a lot of stuff from QBP at a very good rate, which can bring down costs at little to no quality penalties versus using other manufacturer's parts, and can do custom runs for similar products that would be sourced from QBP without accessories or fan fare)
Remember that a lot of bicycling things on part quality is in people's heads. If I took a Deore derailleur and made it to say it was a Tourney derailleur and people saw it, a lot of people would say it performed inherently worse than one that I hadn't scratched out the decal on. Also, there is variance in manufacturing and riding conditions, so sometimes you get a part which lasts longer than others in the same batch or model or comparable models. It would be near impossible to get a statistically significant sample telling you that aftermarket parts are different than the parts with the exact same model number supplied to bike manufacturers (I highly doubt it exists). Also, when someone pays for something, they often believe it to be better than the "free" thing (there was a Newsweek article about 6 years ago talking about how colleges priced themselves higher so people would believe them to be better). We see this all the time with other products in our lives, such as badge-engineered cars among other things. Finally, remember that a lot of people replace things as they break, when typically the parts aren't running well anyway, lowering the perceived quality versus the replacement, even if they are the same part.
There is a practice, commonly used in the electronics industry, known as "binning", where parts are tested, and higher performing parts are often branded differently than lower performing parts and sold at a higher price (and when there are more higher performing parts than necessary, they are often branded as lower quality parts. Overclockers in PC's, for example, often take make use of this for unlocking cores and extra performance and what not). However, it doesn't really make sense to do a binning for lower quality and higher quality things in the same model number and sell the lower quality ones to bike manufacturers - the parts in a bicycle are simple enough from a machining standpoint typically that they can be made amazingly consistent, so the variations aren't great, and this just adds complexity to the supply line as well as opens up the manufacturer to possible litigation.
If they are different models, then sure there can be differences in quality (you may fit a cheaper part for something not very important or a wear item or something that people aren't used to noticing (as noted in one of the comments) like a cassette or saddle (which is hard to choose due to personal preferences) or pedals (often thrown out for some clipless system on more expensive bikes) when build the complete bike, knowing that this will bring your costs down and the rider is going to replace anyway with something better once it wears out). You can save some money this way (and certainly, this is substantial for bicycles which are cheaper).
In any case, I wouldn't lose any sleep over it and would just ride things until they need replacing. Then just pickup whatever looks good and move on with your life.